dnrops.gitlink.net/posts/leetcode/js_leetcode.html

937 lines
58 KiB
HTML
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE HTML>
<html lang="en" class="coal" dir="ltr">
<head>
<!-- Book generated using mdBook -->
<meta charset="UTF-8">
<title>js leetcode - Andrew&#x27;s Blog</title>
<!-- Custom HTML head -->
<meta name="description" content="Andrew Ryan&#x27;s Blog">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="theme-color" content="#ffffff">
<link rel="icon" href="../../favicon.svg">
<link rel="shortcut icon" href="../../favicon.png">
<link rel="stylesheet" href="../../css/variables.css">
<link rel="stylesheet" href="../../css/general.css">
<link rel="stylesheet" href="../../css/chrome.css">
<!-- Fonts -->
<link rel="stylesheet" href="../../FontAwesome/css/font-awesome.css">
<link rel="stylesheet" href="../../fonts/fonts.css">
<!-- Highlight.js Stylesheets -->
<link rel="stylesheet" href="../../highlight.css">
<link rel="stylesheet" href="../../tomorrow-night.css">
<link rel="stylesheet" href="../../ayu-highlight.css">
<!-- Custom theme stylesheets -->
<link rel="stylesheet" href="../../src/style/custom.css">
<!-- MathJax -->
<script async src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
</head>
<body class="sidebar-visible no-js">
<div id="body-container">
<!-- Provide site root to javascript -->
<script>
var path_to_root = "../../";
var default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? "coal" : "coal";
</script>
<!-- Work around some values being stored in localStorage wrapped in quotes -->
<script>
try {
var theme = localStorage.getItem('mdbook-theme');
var sidebar = localStorage.getItem('mdbook-sidebar');
if (theme.startsWith('"') && theme.endsWith('"')) {
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
}
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
}
} catch (e) { }
</script>
<!-- Set the theme before any content is loaded, prevents flash -->
<script>
var theme;
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
if (theme === null || theme === undefined) { theme = default_theme; }
var html = document.querySelector('html');
html.classList.remove('coal')
html.classList.add(theme);
var body = document.querySelector('body');
body.classList.remove('no-js')
body.classList.add('js');
</script>
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
<!-- Hide / unhide sidebar before it is displayed -->
<script>
var body = document.querySelector('body');
var sidebar = null;
var sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
if (document.body.clientWidth >= 1080) {
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
sidebar = sidebar || 'visible';
} else {
sidebar = 'hidden';
}
sidebar_toggle.checked = sidebar === 'visible';
body.classList.remove('sidebar-visible');
body.classList.add("sidebar-" + sidebar);
</script>
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
<div class="sidebar-scrollbox">
<ol class="chapter"><li class="chapter-item affix "><a href="../../index.html">Andrew's Blog</a></li><li class="chapter-item "><a href="../../posts/linux/linux.html"><strong aria-hidden="true">1.</strong> linux</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/linux/install_linux.html"><strong aria-hidden="true">1.1.</strong> install linux</a></li><li class="chapter-item "><a href="../../posts/linux/bash_profile.html"><strong aria-hidden="true">1.2.</strong> bash profile</a></li><li class="chapter-item "><a href="../../posts/linux/command_list.html"><strong aria-hidden="true">1.3.</strong> command list</a></li><li class="chapter-item "><a href="../../posts/linux/git_guide.html"><strong aria-hidden="true">1.4.</strong> git guide</a></li><li class="chapter-item "><a href="../../posts/linux/tar.html"><strong aria-hidden="true">1.5.</strong> tar</a></li><li class="chapter-item "><a href="../../posts/linux/run_x86_elf_in_x64_setup.html"><strong aria-hidden="true">1.6.</strong> run x86 elf in x64 setup</a></li></ol></li><li class="chapter-item "><a href="../../posts/mac/mac.html"><strong aria-hidden="true">2.</strong> mac</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/mac/macos_profiles.html"><strong aria-hidden="true">2.1.</strong> macos profiles</a></li></ol></li><li class="chapter-item "><a href="../../posts/swift/swift.html"><strong aria-hidden="true">3.</strong> swift</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/swift/learn_swift.html"><strong aria-hidden="true">3.1.</strong> learn swift basics</a></li><li class="chapter-item "><a href="../../posts/swift/swift_extensions.html"><strong aria-hidden="true">3.2.</strong> Swift extensions</a></li><li class="chapter-item "><a href="../../posts/swift/swiftui_extension.html"><strong aria-hidden="true">3.3.</strong> SwiftUI extensions</a></li><li class="chapter-item "><a href="../../posts/swift/install_swift.html"><strong aria-hidden="true">3.4.</strong> install swift</a></li><li class="chapter-item "><a href="../../posts/swift/task_planner.html"><strong aria-hidden="true">3.5.</strong> implment task panner app with SwiftUI</a></li><li class="chapter-item "><a href="../../posts/swift/swift_cheat_sheet.html"><strong aria-hidden="true">3.6.</strong> Swift Cheat Sheet</a></li><li class="chapter-item "><a href="../../posts/swift/yinci_url.html"><strong aria-hidden="true">3.7.</strong> Personal privacy protocol</a></li><li class="chapter-item "><a href="../../posts/swift/swift_regular_exressions.html"><strong aria-hidden="true">3.8.</strong> Swift regular exressions</a></li><li class="chapter-item "><a href="../../posts/ios/how_to_create_beautiful_ios_charts_in_swift.html"><strong aria-hidden="true">3.9.</strong> How to Create Beautiful iOS Charts in鑱絊wift</a></li><li class="chapter-item "><a href="../../posts/swift/swiftui_source_code.html"><strong aria-hidden="true">3.10.</strong> SwiftUI source code</a></li><li class="chapter-item "><a href="../../posts/swift/use_swift_fetch_iciba_api.html"><strong aria-hidden="true">3.11.</strong> use swift fetch iciba API</a></li></ol></li><li class="chapter-item "><a href="../../posts/ios/ios.html"><strong aria-hidden="true">4.</strong> ios</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/ios/cocaposd_setup_and_install_for_ios_project.html"><strong aria-hidden="true">4.1.</strong> cocaposd setup and install for ios project</a></li><li class="chapter-item "><a href="../../posts/ios/swiftui_show_gif_image.html"><strong aria-hidden="true">4.2.</strong> SwiftUI show gif image</a></li><li class="chapter-item "><a href="../../posts/ios/implement_task_planner_app.html"><strong aria-hidden="true">4.3.</strong> implement Task planner App</a></li></ol></li><li class="chapter-item "><a href="../../posts/objective_c/objective_c.html"><strong aria-hidden="true">5.</strong> objective_c</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/objective_c/objective_c_cheat_sheet.html"><strong aria-hidden="true">5.1.</strong> Objective-C Cheat Sheet</a></li><li class="chapter-item "><a href="../../posts/objective_c/objective_c_for_absolute_beginners_read_note.html"><strong aria-hidden="true">5.2.</strong> Objective-C Note</a></li></ol></li><li class="chapter-item "><a href="../../posts/dart/dart.html"><strong aria-hidden="true">6.</strong> dart</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/dart/flutter.html"><strong aria-hidden="true">6.1.</strong> Flutter Cheat Sheet</a></li><li class="chapter-item "><a href="../../posts/dart/dart_cheat_sheet.html"><strong aria-hidden="true">6.2.</strong> Dart Cheat Sheet</a></li><li class="chapter-item "><a href="../../posts/flutter/flutter_dev_test.html"><strong aria-hidden="true">6.3.</strong> Flutter dev test</a></li></ol></li><li class="chapter-item "><a href="../../posts/rust/rust.html"><strong aria-hidden="true">7.</strong> rust</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/rust/offline_use_rust.html"><strong aria-hidden="true">7.1.</strong> Offline use rust</a></li><li class="chapter-item "><a href="../../posts/rust/rust_grammer.html"><strong aria-hidden="true">7.2.</strong> rust grammar</a></li><li class="chapter-item "><a href="../../posts/rust/pase_string_and_decimal_conversion.html"><strong aria-hidden="true">7.3.</strong> pase string and decimal conversion</a></li><li class="chapter-item "><a href="../../posts/rust/parse_types.html"><strong aria-hidden="true">7.4.</strong> rust types</a></li><li class="chapter-item "><a href="../../posts/rust/rust_life_cycle.html"><strong aria-hidden="true">7.5.</strong> Rust life cycle</a></li><li class="chapter-item "><a href="../../posts/rust/rust_generic.html"><strong aria-hidden="true">7.6.</strong> rust generics</a></li><li class="chapter-item "><a href="../../posts/rust/rust_implment_matrix.html"><strong aria-hidden="true">7.7.</strong> Rust implement matrix</a></li><li class="chapter-item "><a href="../../posts/rust/rust_sort.html"><strong aria-hidden="true">7.8.</strong> Rust implement sort algorithms</a></li><li class="chapter-item "><a href="../../posts/rust/implement_aes_encryption.html"><strong aria-hidden="true">7.9.</strong> Rust implement AEC encryption and decryption</a></li><li class="chapter-item "><a href="../../posts/rust/implement_trie_data_structure.html"><strong aria-hidden="true">7.10.</strong> implement trie data structure</a></li><li class="chapter-item "><a href="../../posts/rust/rust_implement_tree.html"><strong aria-hidden="true">7.11.</strong> implement tree data_structure</a></li><li class="chapter-item "><a href="../../posts/rust/list_dir.html"><strong aria-hidden="true">7.12.</strong> list dir</a></li><li class="chapter-item "><a href="../../posts/rust/fast_way_to_implment_object_trait.html"><strong aria-hidden="true">7.13.</strong> fast way to implment object trait</a></li><li class="chapter-item "><a href="../../posts/rust/compress_rust_binary_size.html"><strong aria-hidden="true">7.14.</strong> compress rust binary size</a></li><li class="chapter-item "><a href="../../posts/rust/implment_file_upload_backend.html"><strong aria-hidden="true">7.15.</strong> impliment file upload</a></li><li class="chapter-item "><a href="../../posts/rust/this_is_add_post_cli_implementation_in_rust.html"><strong aria-hidden="true">7.16.</strong> this is add_post cli implementation in rust</a></li><li class="chapter-item "><a href="../../posts/rust/use_rust_implment_a_copyclipbord_cli.html"><strong aria-hidden="true">7.17.</strong> Use rust implment a copyclipbord CLI</a></li><li class="chapter-item "><a href="../../posts/rust/sqlite_database_add_delete_update_show_in_rust.html"><strong aria-hidden="true">7.18.</strong> sqlite database add delete update show in rust</a></li><li class="chapter-item "><a href="../../posts/rust/implementing_tokio_joinhandle_for_wasm.html"><strong aria-hidden="true">7.19.</strong> Implementing tokio JoinHandle for wasm</a></li><li class="chapter-item "><a href="../../posts/rust/rust_implement_a_crate_for_encode_and_decode_brainfuck_and_ook.html"><strong aria-hidden="true">7.20.</strong> rust implement a crate for encode and decode brainfuck and ook</a></li><li class="chapter-item "><a href="../../posts/rust/slint_builtin_elements.html"><strong aria-hidden="true">7.21.</strong> Slint Builtin Elements</a></li><li class="chapter-item "><a href="../../posts/rust/corporate_network_install_rust_on_windows.html"><strong aria-hidden="true">7.22.</strong> Corporate network install Rust on windows</a></li><li class="chapter-item "><a href="../../posts/rust/rust_binary_file_how_to_judge_static_link_or_dynamic_link_in_macos.html"><strong aria-hidden="true">7.23.</strong> rust binary file how to judge static link or dynamic link in Macos</a></li><li class="chapter-item "><a href="../../posts/rust/rust_binary_include_dir_and_get_contents.html"><strong aria-hidden="true">7.24.</strong> rust binary include dir and get contents</a></li><li class="chapter-item "><a href="../../posts/rust/rust_logger_non-block.html"><strong aria-hidden="true">7.25.</strong> rust logger non-block</a></li><li class="chapter-item "><a href="../../posts/rust/rust_connect_sql_server_database.html"><strong aria-hidden="true">7.26.</strong> rust connect sql server database</a></li><li class="chapter-item "><a href="../../posts/rust/rust_websocket_implment.html"><strong aria-hidden="true">7.27.</strong> rust websocket implment</a></li></ol></li><li class="chapter-item "><a href="../../posts/java/java.html"><strong aria-hidden="true">8.</strong> java</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/java/java_grammar.html"><strong aria-hidden="true">8.1.</strong> java grammar and codewar</a></li><li class="chapter-item "><a href="../../posts/java/run_jar.html"><strong aria-hidden="true">8.2.</strong> java run .jar</a></li><li class="chapter-item "><a href="../../posts/java/java_pomxml_add_defaultgoal_to_build.html"><strong aria-hidden="true">8.3.</strong> Java pomxml add defaultGoal to build</a></li><li class="chapter-item "><a href="../../posts/java/java_set_mvn_mirror.html"><strong aria-hidden="true">8.4.</strong> Java set mvn mirror</a></li></ol></li><li class="chapter-item "><a href="../../posts/python/python.html"><strong aria-hidden="true">9.</strong> python</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/python/convert_pesn.html"><strong aria-hidden="true">9.1.</strong> convert pesn</a></li><li class="chapter-item "><a href="../../posts/python/find_remove_dir.html"><strong aria-hidden="true">9.2.</strong> find and remove dir</a></li><li class="chapter-item "><a href="../../posts/python/timing_message.html"><strong aria-hidden="true">9.3.</strong> wechat send message</a></li><li class="chapter-item "><a href="../../posts/python/use_python_openpyxl_package_read_and_edit_excel_files.html"><strong aria-hidden="true">9.4.</strong> Use python openpyxl package read and edit excel files</a></li></ol></li><li class="chapter-item "><a href="../../posts/go/go.html"><strong aria-hidden="true">10.</strong> go</a></li><li class="chapter-item "><a href="../../posts/js/js.html"><strong aria-hidden="true">11.</strong> js</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/js/js_tutorial.html"><strong aria-hidden="true">11.1.</strong> js tutorial</a></li><li class="chapter-item "><a href="../../posts/js/js_tutorial_map.html"><strong aria-hidden="true">11.2.</strong> ja map</a></li><li class="chapter-item "><a href="../../posts/js/js_tutorial_math.html"><strong aria-hidden="true">11.3.</strong> js math</a></li><li class="chapter-item "><a href="../../posts/js/js_tutorial_object.html"><strong aria-hidden="true">11.4.</strong> js object</a></li><li class="chapter-item "><a href="../../posts/js/js_tutorial_set.html"><strong aria-hidden="true">11.5.</strong> js set</a></li><li class="chapter-item "><a href="../../posts/js/single_thread_and_asynchronous.html"><strong aria-hidden="true">11.6.</strong> single thread and asynchronous</a></li><li class="chapter-item "><a href="../../posts/js/this.html"><strong aria-hidden="true">11.7.</strong> js this</a></li><li class="chapter-item "><a href="../../posts/js/js_implment_aes.html"><strong aria-hidden="true">11.8.</strong> js implment aes</a></li><li class="chapter-item "><a href="../../posts/js/getting_started_with_ajax.html"><strong aria-hidden="true">11.9.</strong> getting started with ajax</a></li><li class="chapter-item "><a href="../../posts/js/BinarySearchTree.html"><strong aria-hidden="true">11.10.</strong> binary search tree</a></li><li class="chapter-item "><a href="../../posts/js/goole_zx.html"><strong aria-hidden="true">11.11.</strong> goole zx</a></li><li class="chapter-item "><a href="../../posts/js/es6.html"><strong aria-hidden="true">11.12.</strong> es6</a></li></ol></li><li class="chapter-item "><a href="../../posts/ruby/ruby.html"><strong aria-hidden="true">12.</strong> ruby</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/ruby/rails_setup_env.html"><strong aria-hidden="true">12.1.</strong> ruby on rails setup environment</a></li><li class="chapter-item "><a href="../../posts/ruby/learn_ruby.html"><strong aria-hidden="true">12.2.</strong> learn ruby</a></li><li class="chapter-item "><a href="../../posts/ruby/ruby_note.html"><strong aria-hidden="true">12.3.</strong> Ruby Note</a></li><li class="chapter-item "><a href="../../posts/ruby/setup_ruby_for_ctf.html"><strong aria-hidden="true">12.4.</strong> Setup ruby for CTF</a></li></ol></li><li class="chapter-item "><a href="../../posts/react/react.html"><strong aria-hidden="true">13.</strong> react</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/react/react_life_cycle.html"><strong aria-hidden="true">13.1.</strong> react life cycle</a></li><li class="chapter-item "><a href="../../posts/react/react_router.html"><strong aria-hidden="true">13.2.</strong> react router</a></li><li class="chapter-item "><a href="../../posts/react/react_this.html"><strong aria-hidden="true">13.3.</strong> react this</a></li><li class="chapter-item "><a href="../../posts/react/react_interviw.html"><strong aria-hidden="true">13.4.</strong> react interview</a></li><li class="chapter-item "><a href="../../posts/react/important_react_interview.html"><strong aria-hidden="true">13.5.</strong> important react interview</a></li><li class="chapter-item "><a href="../../posts/react/react_quick_reference.html"><strong aria-hidden="true">13.6.</strong> react quick reference</a></li><li class="chapter-item "><a href="../../posts/react/redux_quick_reference.html"><strong aria-hidden="true">13.7.</strong> redux quick reference</a></li></ol></li><li class="chapter-item "><a href="../../posts/vue/vue.html"><strong aria-hidden="true">14.</strong> vue</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/vue/vue_ajax.html"><strong aria-hidden="true">14.1.</strong> vue ajax</a></li></ol></li><li class="chapter-item "><a href="../../posts/angular/angular.html"><strong aria-hidden="true">15.</strong> angular</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/angular/controller_communication.html"><strong aria-hidden="true">15.1.</strong> controller communication</a></li><li class="chapter-item "><a href="../../posts/angular/creating_custom_directives.html"><strong aria-hidden="true">15.2.</strong> creating custom directives</a></li><li class="chapter-item "><a href="../../posts/angular/directive_notes.html"><strong aria-hidden="true">15.3.</strong> directive notes</a></li><li class="chapter-item "><a href="../../posts/angular/directive_communication.html"><strong aria-hidden="true">15.4.</strong> directive communication</a></li><li class="chapter-item "><a href="../../posts/angular/post_params.html"><strong aria-hidden="true">15.5.</strong> post params</a></li><li class="chapter-item "><a href="../../posts/angular/read_json_angular.html"><strong aria-hidden="true">15.6.</strong> read json angular</a></li><li class="chapter-item "><a href="../../posts/angular/same_route_reload.html"><strong aria-hidden="true">15.7.</strong> same route reload</a></li></ol></li><li class="chapter-item "><a href="../../posts/css/css.html"><strong aria-hidden="true">16.</strong> css</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/css/use_css_media.html"><strong aria-hidden="true">16.1.</strong> use css media</a></li></ol></li><li class="chapter-item "><a href="../../posts/php/php.html"><strong aria-hidden="true">17.</strong> php</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/php/for_php_string_implment_some_extemtion_functions.html"><strong aria-hidden="true">17.1.</strong> for php string implment some extemtion functions</a></li><li class="chapter-item "><a href="../../posts/php/php_cheatsheet.html"><strong aria-hidden="true">17.2.</strong> PHP cheatsheet</a></li></ol></li><li class="chapter-item expanded "><a href="../../posts/leetcode/leetcode.html"><strong aria-hidden="true">18.</strong> leetcode</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/leetcode/rust_leetcode.html"><strong aria-hidden="true">18.1.</strong> rust leetcode</a></li><li class="chapter-item "><a href="../../posts/leetcode/rust_codewar.html"><strong aria-hidden="true">18.2.</strong> rust codewar</a></li><li class="chapter-item "><a href="../../posts/leetcode/swift_codewar.html"><strong aria-hidden="true">18.3.</strong> swift codewar</a></li><li class="chapter-item expanded "><a href="../../posts/leetcode/js_leetcode.html" class="active"><strong aria-hidden="true">18.4.</strong> js leetcode</a></li><li class="chapter-item "><a href="../../posts/leetcode/java_leetcode.html"><strong aria-hidden="true">18.5.</strong> java leetcode</a></li><li class="chapter-item "><a href="../../posts/leetcode/rust_huawei.html"><strong aria-hidden="true">18.6.</strong> huawei test</a></li><li class="chapter-item "><a href="../../posts/leetcode/rust_utils.html"><strong aria-hidden="true">18.7.</strong> rust common functions</a></li><li class="chapter-item "><a href="../../posts/leetcode/olympiad_training.html"><strong aria-hidden="true">18.8.</strong> Computer olympiad training</a></li></ol></li><li class="chapter-item "><a href="../../posts/ctf/CTF.html"><strong aria-hidden="true">19.</strong> ctf</a><a class="toggle"><div></div></a></li><li><ol class="section"><li class="chapter-item "><a href="../../posts/ctf/CTF_Note.html"><strong aria-hidden="true">19.1.</strong> CTF Note</a></li><li class="chapter-item "><a href="../../posts/ctf/0.1_Web.html"><strong aria-hidden="true">19.2.</strong> Web</a></li><li class="chapter-item "><a href="../../posts/ctf/4.1_Misc.html"><strong aria-hidden="true">19.3.</strong> Misc</a></li><li class="chapter-item "><a href="../../posts/ctf/3.2_PWN_note.html"><strong aria-hidden="true">19.4.</strong> PWN</a></li><li class="chapter-item "><a href="../../posts/ctf/3.1_Crypto.html"><strong aria-hidden="true">19.5.</strong> Crypto</a></li><li class="chapter-item "><a href="../../posts/ctf/3.4_RSA_note.html"><strong aria-hidden="true">19.6.</strong> Rsa attack</a></li><li class="chapter-item "><a href="../../posts/ctf/3.5_Base64.html"><strong aria-hidden="true">19.7.</strong> Base64</a></li><li class="chapter-item "><a href="../../posts/ctf/0.0_SQL Injection Cheatsheet.html"><strong aria-hidden="true">19.8.</strong> SQL Injection Cheatsheet</a></li><li class="chapter-item "><a href="../../posts/ctf/1.1_SQL_injection.html"><strong aria-hidden="true">19.9.</strong> SQL Injection</a></li><li class="chapter-item "><a href="../../posts/ctf/1.2_SQL_injection_UNION_attacks.html"><strong aria-hidden="true">19.10.</strong> SQL Injection UNION attacks</a></li><li class="chapter-item "><a href="../../posts/ctf/1.3_Blind SQL injection.html"><strong aria-hidden="true">19.11.</strong> Blind SQL Injection</a></li><li class="chapter-item "><a href="../../posts/ctf/1.4_Code Injection.html"><strong aria-hidden="true">19.12.</strong> Code Injection</a></li><li class="chapter-item "><a href="../../posts/ctf/1.5_SSRF.html"><strong aria-hidden="true">19.13.</strong> SSRF</a></li><li class="chapter-item "><a href="../../posts/ctf/1.6_OS command injection.html"><strong aria-hidden="true">19.14.</strong> OS command injection</a></li><li class="chapter-item "><a href="../../posts/ctf/1.7_Local file inclusion.html"><strong aria-hidden="true">19.15.</strong> Local file inclusion</a></li><li class="chapter-item "><a href="../../posts/ctf/1.8_Remote file inclusion.html"><strong aria-hidden="true">19.16.</strong> Remote file inclusion</a></li><li class="chapter-item "><a href="../../posts/ctf/1.9_CSRFm.html"><strong aria-hidden="true">19.17.</strong> CSRF</a></li><li class="chapter-item "><a href="../../posts/ctf/1.10_NoSQL injection.html"><strong aria-hidden="true">19.18.</strong> NoSQL injection</a></li><li class="chapter-item "><a href="../../posts/ctf/1.11_JSON injection.html"><strong aria-hidden="true">19.19.</strong> JSON injection</a></li><li class="chapter-item "><a href="../../posts/ctf/1.12_CTF_Web_SQL_Note.html"><strong aria-hidden="true">19.20.</strong> CTF Web SQL Note</a></li><li class="chapter-item "><a href="../../posts/ctf/2.1_XXE.html"><strong aria-hidden="true">19.21.</strong> XXE</a></li><li class="chapter-item "><a href="../../posts/ctf/2.2_XSS.html"><strong aria-hidden="true">19.22.</strong> XSS</a></li><li class="chapter-item "><a href="../../posts/ctf/2.3_Upload File.html"><strong aria-hidden="true">19.23.</strong> Upload File</a></li><li class="chapter-item "><a href="../../posts/ctf/2.4_serialize_unserialize.html"><strong aria-hidden="true">19.24.</strong> serialize unserialize</a></li><li class="chapter-item "><a href="../../posts/ctf/2.5_Race condition.html"><strong aria-hidden="true">19.25.</strong> Race condition</a></li><li class="chapter-item "><a href="../../posts/ctf/3.2_PWN_note.html"><strong aria-hidden="true">19.26.</strong> PWN_note</a></li><li class="chapter-item "><a href="../../posts/ctf/3.3_pwn HCTF2016 brop.html"><strong aria-hidden="true">19.27.</strong> pwn HCTF2016 brop</a></li><li class="chapter-item "><a href="../../posts/ctf/pwn_patch_defense_skill.html"><strong aria-hidden="true">19.28.</strong> PWN Patch defense skill</a></li><li class="chapter-item "><a href="../../posts/ctf/pwn_stack_overflow.html"><strong aria-hidden="true">19.29.</strong> PWN stack overflow</a></li><li class="chapter-item "><a href="../../posts/ctf/pwn_heap_overflow.html"><strong aria-hidden="true">19.30.</strong> PWN heap overflow</a></li><li class="chapter-item "><a href="../../posts/ctf/pwn_format_string_vulnerability.html"><strong aria-hidden="true">19.31.</strong> PWN Format String Vulnerability</a></li><li class="chapter-item "><a href="../../posts/ctf/kali_linux_tutorials.html"><strong aria-hidden="true">19.32.</strong> Kali linux tutorials</a></li><li class="chapter-item "><a href="../../posts/ctf/google_dorks_2023_lists.html"><strong aria-hidden="true">19.33.</strong> Google Dorks 2023 Lists</a></li><li class="chapter-item "><a href="../../posts/ctf/dvwa_writeup.html"><strong aria-hidden="true">19.34.</strong> DVWA WriteUp</a></li><li class="chapter-item "><a href="../../posts/ctf/bwapp_writeup.html"><strong aria-hidden="true">19.35.</strong> bWAPP WriteUp</a></li><li class="chapter-item "><a href="../../posts/ctf/sqlilabs_writeup.html"><strong aria-hidden="true">19.36.</strong> sqlilabs WriteUp</a></li><li class="chapter-item "><a href="../../posts/ctf/ctf_train_at_hangzhou.html"><strong aria-hidden="true">19.37.</strong> ctf train at hangzhou</a></li><li class="chapter-item "><a href="../../posts/ctf/ctf_common_mindmap_list.html"><strong aria-hidden="true">19.38.</strong> ctf common mindmap list</a></li><li class="chapter-item "><a href="../../posts/ctf/error_based_sql_injection.html"><strong aria-hidden="true">19.39.</strong> Error Based SQL Injection</a></li><li class="chapter-item "><a href="../../posts/ctf/urlfinder_tutorial.html"><strong aria-hidden="true">19.40.</strong> URLFinder Tutorial</a></li><li class="chapter-item "><a href="../../posts/ctf/observer_ward_tutorial.html"><strong aria-hidden="true">19.41.</strong> observer_ward Tutorial</a></li><li class="chapter-item "><a href="../../posts/ctf/mysql_udf_.html"><strong aria-hidden="true">19.42.</strong> MySQL UDF 提权</a></li><li class="chapter-item "><a href="../../posts/ctf/nuclei__tutorial.html"><strong aria-hidden="true">19.43.</strong> Nuclei Tutorial</a></li><li class="chapter-item "><a href="../../posts/ctf/2024_ctf_solution_thinking.html"><strong aria-hidden="true">19.44.</strong> 2024 ctf solution thinking</a></li><li class="chapter-item "><a href="../../posts/ctf/man_che_si_te_bian_ma.html"><strong aria-hidden="true">19.45.</strong> 曼彻斯特编码</a></li></ol></li></ol>
</div>
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
<div class="sidebar-resize-indicator"></div>
</div>
</nav>
<!-- Track and set sidebar scroll position -->
<script>
var sidebarScrollbox = document.querySelector('#sidebar .sidebar-scrollbox');
sidebarScrollbox.addEventListener('click', function(e) {
if (e.target.tagName === 'A') {
sessionStorage.setItem('sidebar-scroll', sidebarScrollbox.scrollTop);
}
}, { passive: true });
var sidebarScrollTop = sessionStorage.getItem('sidebar-scroll');
sessionStorage.removeItem('sidebar-scroll');
if (sidebarScrollTop) {
// preserve sidebar scroll position when navigating via links within sidebar
sidebarScrollbox.scrollTop = sidebarScrollTop;
} else {
// scroll sidebar to current active section when navigating via "next/previous chapter" buttons
var activeSection = document.querySelector('#sidebar .active');
if (activeSection) {
activeSection.scrollIntoView({ block: 'center' });
}
}
</script>
<div id="page-wrapper" class="page-wrapper">
<div class="page">
<div id="menu-bar-hover-placeholder"></div>
<div id="menu-bar" class="menu-bar sticky">
<div class="left-buttons">
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
<i class="fa fa-bars"></i>
</label>
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
<i class="fa fa-paint-brush"></i>
</button>
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
</ul>
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
<i class="fa fa-search"></i>
</button>
</div>
<h1 class="menu-title">Andrew&#x27;s Blog</h1>
<div class="right-buttons">
<a href="https://gitlink.org.cn/dnrops/dnrops.gitlink.net.git" title="Git repository" aria-label="Git repository">
<i id="git-repository-button" class="fa fa-github"></i>
</a>
</div>
</div>
<div id="search-wrapper" class="hidden">
<form id="searchbar-outer" class="searchbar-outer">
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
</form>
<div id="searchresults-outer" class="searchresults-outer hidden">
<div id="searchresults-header" class="searchresults-header"></div>
<ul id="searchresults">
</ul>
</div>
</div>
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
<script>
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
});
</script>
<div id="content" class="content">
<main>
<h1 id="leetcode-js-implementation"><a class="header" href="#leetcode-js-implementation">Leetcode js implementation</a></h1>
<h2 id="string-中部分字母替换成大写"><a class="header" href="#string-中部分字母替换成大写">String 中部分字母替换成大写</a></h2>
<pre><code class="language-js">let inputArray = "Who Love Solo OOO".split(" ");
let lower = ["a", "e", "i", "o", "u"]; //替换这些字母为大写
function doFunc(arr) {
let new_arr = [];
let new_arr_join = arr.join().toLowerCase().split("");
// console.log(new_arr_join);
for (let i = 0; i &lt; new_arr_join.length; i++) {
if (lower.includes(new_arr_join[i])) {
new_arr.push(new_arr_join[i].toUpperCase());
} else {
new_arr.push(new_arr_join[i]);
}
}
console.log(new_arr.join("").replace(/,/g, " "));
}
doFunc(inputArray); //whO lOvE sOlO OOO
</code></pre>
<h2 id="用-string-加很大的数add-large-num-use-string"><a class="header" href="#用-string-加很大的数add-large-num-use-string">用 String 加很大的数add large num use string</a></h2>
<pre><code class="language-js">let sum_string = function (num1, num2) {
let i = num1.length - 1,
j = num2.length - 1,
add = 0;
const ans = [];
while (i &gt;= 0 || j &gt;= 0 || add != 0) {
let a = 0,
b = 0;
if (i &gt;= 0) {
a = num1.charAt(i) - "0";
} else {
a = 0;
}
if (j &gt;= 0) {
b = num2.charAt(j) - "0";
} else {
b = 0;
}
let re = a + b + add;
ans.push(re % 10);
add = Math.floor(re / 10);
i -= 1;
j -= 1;
}
return ans.reverse().join("");
};
let a = "99999999999999999999999999999999999999999999999999999999999";
let b = "999999999999999999999999999999999999999999999999999999999";
let result = sum_string(a, b);
console.log(result);
</code></pre>
<h2 id="数组反转"><a class="header" href="#数组反转">数组反转</a></h2>
<pre><code class="language-js">const reverse = (arr) =&gt; {
return arr.reverse().join("");
};
</code></pre>
<h2 id="对-string-全排列-并删除-相同重复元素输出最终不同的全排列个数"><a class="header" href="#对-string-全排列-并删除-相同重复元素输出最终不同的全排列个数">对 string 全排列 并删除 相同(重复)元素,输出最终不同的全排列个数</a></h2>
<pre><code class="language-js">let inputArray = "ABCDEFGHHA".split("");
//删除数组中相同(重复)的元素
function remove(arr) {
return Array.from(new Set(arr));
}
//全排列
function permutate(str) {
var result = [];
if (str.length == 1) {
return [str];
} else {
var preResult = permutate(str.slice(1));
for (var j = 0; j &lt; preResult.length; j++) {
for (var k = 0; k &lt; preResult[j].length + 1; k++) {
var temp = preResult[j].slice(0, k) + str[0] + preResult[j].slice(k);
result.push(temp);
}
}
return result;
}
}
//n jiecheng
function n_(x) {
if (x &lt; 2) {
return 1;
} else {
return x * n_(x - 1);
}
}
function doFunc(arr) {
let join_arr = arr.join("");
console.log(remove(permutate(join_arr)).length);
}
doFunc(inputArray);
// ABA,AAB,BAA
</code></pre>
<h2 id="数组小到大排序"><a class="header" href="#数组小到大排序">数组小到大排序</a></h2>
<pre><code class="language-js">function(arr){
return arr = arr.sort((a, b) =&gt; a[0] - b[0]);
}
</code></pre>
<h2 id="输出数的因子乘积等于该数的-数组"><a class="header" href="#输出数的因子乘积等于该数的-数组">输出数的因子乘积等于该数的 数组</a></h2>
<pre><code class="language-js">const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.question("", function (line) {
let number = parseInt(line);
factor(number);
rl.close();
});
function factor(num) {
let result = "";
for (let i = 2; i * i &lt;= num &amp;&amp; i &lt;= num; i++) {
while (num % i === 0) {
result += i + " ";
num /= i;
}
}
if (num &gt; 1) result += num + " ";
console.log(result);
}
</code></pre>
<h2 id="给定一个数组和一个目标和从数组中找两个数字相加等于目标和输出这两个数字的下标"><a class="header" href="#给定一个数组和一个目标和从数组中找两个数字相加等于目标和输出这两个数字的下标">给定一个数组和一个目标和,从数组中找两个数字相加等于目标和,输出这两个数字的下标。</a></h2>
<pre><code class="language-js">function twoSum(nums, target) {
let ans = [];
for (let i = 0; i &lt; nums.length; i++) {
for (let j = i + 1; j &lt; nums.length; j++) {
if (nums[i] + nums[j] == target) {
ans[0] = [i, nums[i]];
ans[1] = [j, nums[j]];
return ans;
}
}
}
return ans;
}
let nums = [0, 0, 0, 5, 6, 7, 8];
let target = 9;
console.log(twoSum(nums, target));
</code></pre>
<h2 id="插入排序"><a class="header" href="#插入排序">插入排序</a></h2>
<pre><code class="language-js">//小到大
function insertionSort(arr) {
var len = arr.length;
var preIndex, current;
for (var i = 1; i &lt; len; i++) {
preIndex = i - 1;
current = arr[i];
while (preIndex &gt;= 0 &amp;&amp; arr[preIndex] &gt; current) {
arr[preIndex + 1] = arr[preIndex];
preIndex--;
}
arr[preIndex + 1] = current;
}
return arr;
}
let nums = [0, 20, 0, 5, 6, 7, 8];
console.log(insertionSort(nums));
</code></pre>
<h2 id="选择排序"><a class="header" href="#选择排序">选择排序</a></h2>
<pre><code class="language-js">function selectionSort(arr) {
var len = arr.length;
var minIndex, temp;
for (var i = 0; i &lt; len - 1; i++) {
minIndex = i;
for (var j = i + 1; j &lt; len; j++) {
if (arr[j] &lt; arr[minIndex]) {
// 寻找最小的数
minIndex = j; // 将最小数的索引保存
}
}
temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
return arr;
}
</code></pre>
<h2 id="进制转换"><a class="header" href="#进制转换">进制转换</a></h2>
<pre><code class="language-js">// 如果要处理2进制到10进制16进制到10进制8进制到10进制 需要用了paresInt这个方法
//2进制到10进制
parseInt(10, 2); //=&gt;2
//2进制到10进制
parseInt(100, 2); //=&gt;4
//16进制到10进制
parseInt(12, 16); //=&gt;18
//8进制到10进制
parseInt(12, 8); //=&gt;10
//js的进制转换 分为2进制8进制10进制16进制之间的相互转换 我们直接利用 对象.toString()即可实现:
//10进制转为16进制
(10)
.toString(16)(
// =&gt;"a"
//8进制转为16进制
012,
)
.toString(16)(
// =&gt;"a"
//16进制转为10进制
0x16,
)
.toString(10)(
// =&gt;"22"
//16进制转为8进制
0x16,
)
.toString(8)(
// =&gt;"26"
//10进制转为2进制 //=&gt;
1111,
)
.toString(2)(
// =&gt; "10001010111"
//8进制转为2进制 //=&gt;
01111,
)
.toString(2)(
//=&gt;"1001001001"
//16进制转为2进制 //=&gt;
0x16,
)
.toString(2); // =&gt; "10110"
</code></pre>
<h2 id="删除数组中相同重复的元素"><a class="header" href="#删除数组中相同重复的元素">删除数组中相同(重复)的元素</a></h2>
<pre><code class="language-js">const arr = [5, 1, 5, 7, 7, 5];
const unique = [...new Set(arr)]; // [ 5, 1, 7 ]
</code></pre>
<h2 id="js-中能改变值的二进制数组"><a class="header" href="#js-中能改变值的二进制数组">js 中能改变值的二进制数组</a></h2>
<pre><code class="language-js">// Element type Bytes Description C type
// Int8 1 8-bit signed integer signed char
// Uint8 1 8-bit unsigned integer unsigned char
// Uint8C 1 8-bit unsigned integer (clamped conversion) unsigned char
// Int16 2 16-bit signed integer short
// Uint16 2 16-bit unsigned integer unsigned short
// Int32 4 32-bit signed integer int
// Uint32 4 32-bit unsigned integer unsigned int
// Float32 4 32-bit floating point float
// Float64 8 64-bit floating point double
const typedArray = new Float64Array([0, 1, 2]); //64位 8byte
console.log(typedArray.length); // 3
typedArray[0] = 5;
const normalArray = [...typedArray]; // [5,1,2]
// The elements are stored in typedArray.buffer.
// Get a different view on the same data:
const dataView = new DataView(typedArray.buffer);
console.log(dataView.getUint8(0)); // 5
</code></pre>
<h2 id="按照从右向左的阅读顺序返回一个不含重复数字的新的整数"><a class="header" href="#按照从右向左的阅读顺序返回一个不含重复数字的新的整数">按照从右向左的阅读顺序,返回一个不含重复数字的新的整数</a></h2>
<h3 id="hj9-提取不重复的整数"><a class="header" href="#hj9-提取不重复的整数">HJ9 提取不重复的整数</a></h3>
<pre><code class="language-js">let line = "2752771".split("").reverse().join("").split("");
while ((line = readline())) {
var lines = line.split("").reverse().join("").split("");
function remove(arr) {
return [...new Set(arr)];
}
print(remove(lines).join(""));
}
</code></pre>
<h2 id="字符转化为按-ascii-码ascii-码"><a class="header" href="#字符转化为按-ascii-码ascii-码">字符转化为按 ascii 码(ASCII 码)</a></h2>
<pre><code class="language-js">let str = "a".charCodeAt();
console.log(str); //97
</code></pre>
<h2 id="hj10-字符个数统计"><a class="header" href="#hj10-字符个数统计">HJ10 字符个数统计</a></h2>
<h3 id="对于字符串-abaca-而言有-abc-三种不同的字符因此输出-3"><a class="header" href="#对于字符串-abaca-而言有-abc-三种不同的字符因此输出-3">对于字符串 abaca 而言,有 a、b、c 三种不同的字符,因此输出 3。</a></h3>
<h3 id="输出-输入字符串-中范围在0127包括-0-和-127字符的种数"><a class="header" href="#输出-输入字符串-中范围在0127包括-0-和-127字符的种数">输出 输入字符串 中范围在(0~127包括 0 和 127)字符的种数。</a></h3>
<pre><code class="language-js">while ((line = readline())) {
var lines = line.split("");
let count = 0;
function remove(arr) {
return [...new Set(arr)];
}
lines = remove(lines);
for (let i = 0; i &lt; lines.length; i++) {
if (lines[i].charCodeAt() &gt;= 0 &amp;&amp; lines[i].charCodeAt() &lt;= 127) {
// console.log(lines[i]);
count++;
}
}
print(count);
// console.log(count);
}
</code></pre>
<h2 id="数字颠倒"><a class="header" href="#数字颠倒">数字颠倒</a></h2>
<pre><code class="language-js">while ((line = readline())) {
var lines = line.split("");
console.log(lines.reverse().join(""));
// print();
}
</code></pre>
<h2 id="hj12-字符串反转"><a class="header" href="#hj12-字符串反转">HJ12 字符串反转</a></h2>
<h3 id="nodejs-实现"><a class="header" href="#nodejs-实现">node.js 实现</a></h3>
<pre><code class="language-js">const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on("line", function (line) {
const tokens = line.split("");
console.log(tokens.reverse().join(""));
});
const reverse = (arr) =&gt; {
return arr.reverse().join("");
};
</code></pre>
<h2 id="hj13-句子逆序"><a class="header" href="#hj13-句子逆序">HJ13 句子逆序</a></h2>
<p>输入一个英文语句,每个单词用空格隔开。保证输入只包含空格和字母。
输出描述: 得到逆序的句子</p>
<pre><code class="language-js">const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
rl.on("line", function (line) {
const tokens = line.split(" ");
console.log(tokens.reverse().join(" "));
});
const reverse = (arr) =&gt; {
return arr.reverse().join("");
};
</code></pre>
<h2 id="hj14-字符串排序"><a class="header" href="#hj14-字符串排序">HJ14 字符串排序</a></h2>
<pre><code class="language-js">const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let i = 0;
var long = 0;
rl.question("", function (line) {
long = parseInt(line);
});
var arr = [];
rl.on("line", function (line) {
i += 1; //每输入完一行i++
arr.push(line);
if (i == long) {
//等于第一行的输入值的时候停止输入
for (var j = 0; j &lt; arr.length; j++) {
console.log(sort_arr(arr)[j]);
}
rl.close();
}
});
const sort_arr = (arr) =&gt; {
return arr.sort(); //按字母排序
};
</code></pre>
<h2 id="hj15-求-int-型正整数在内存中存储时-1-的个数"><a class="header" href="#hj15-求-int-型正整数在内存中存储时-1-的个数">HJ15 求 int 型正整数在内存中存储时 1 的个数</a></h2>
<pre><code class="language-js">//node.js
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let i;
rl.question("", (line) =&gt; {
i = parseInt(line).toString(2); //转化为2进制
let count = 0;
i.toString()
.split("")
.forEach((e) =&gt; {
if (e === "1") {
count += 1;
}
});
console.log(count);
rl.close();
});
//rust
fn main() {
println!("{}",count4(300));
println!("");
}
fn count4(mut n:i32) -&gt; i32 {
n = (n &amp; 0x55555555) +((n&gt;&gt;1)&amp;0x55555555);
n = (n &amp; 0x33333333) +((n&gt;&gt;2)&amp;0x33333333);
n = (n &amp; 0x0f0f0f0f) +((n&gt;&gt;4)&amp;0x0f0f0f0f);
n = (n &amp; 0x00ff00ff) +((n&gt;&gt;8)&amp;0x00ff00ff);
n = (n &amp; 0x0000ffff) +((n&gt;&gt;16)&amp;0x0000ffff);
return n
}
</code></pre>
<h2 id="合并两个有序链表"><a class="header" href="#合并两个有序链表">合并两个有序链表</a></h2>
<pre><code class="language-java">/**
* Definition for singly-linked list.
* public class ListNode {
* int val;
* ListNode next;
* ListNode() {}
* ListNode(int val) { this.val = val; }
* ListNode(int val, ListNode next) { this.val = val; this.next = next; }
* }
*/
class Solution {
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
ListNode h = new ListNode(0);
ListNode ans=h;
while (l1 != null &amp;&amp; l2 != null) {
if (l1.val &lt; l2.val) {
h.next = l1;
h = h.next;
l1 = l1.next;
} else {
h.next = l2;
h = h.next;
l2 = l2.next;
}
}
if(l1==null){
h.next=l2;
}
if(l2==null){
h.next=l1;
}
return ans.next;
}
}
</code></pre>
<h2 id="数组中删除-element"><a class="header" href="#数组中删除-element">数组中删除 element</a></h2>
<pre><code class="language-java">public int removeElement(int[] nums, int val) {
int i = 0;
int n = nums.length;
while (i &lt; n) {
if (nums[i] == val) {
nums[i] = nums[n - 1];
n--;
} else {
i++;
}
}
return n;
}
</code></pre>
<h2 id="list-全排列"><a class="header" href="#list-全排列">List 全排列</a></h2>
<pre><code class="language-java">class Solution {
public List&lt;List&lt;Integer&gt;&gt; permute(int[] nums) {
List&lt;List&lt;Integer&gt;&gt; all = new ArrayList&lt;&gt;();
//从下标 0 开始的所有组合
upset(nums, 0, all);
return all;
}
private void upset(int[] nums, int begin, List&lt;List&lt;Integer&gt;&gt; all) {
if (begin == nums.length) {
ArrayList&lt;Integer&gt; temp = new ArrayList&lt;Integer&gt;();
for (int i = 0; i &lt; nums.length; i++) {
temp.add(nums[i]);
}
all.add(new ArrayList&lt;Integer&gt;(temp));
return;
}
for (int i = begin; i &lt; nums.length; i++) {
swap(nums, i, begin);
upset(nums, begin + 1, all);
swap(nums, i, begin);
}
}
private void swap(int[] nums, int i, int begin) {
int temp = nums[i];
nums[i] = nums[begin];
nums[begin] = temp;
}
}
</code></pre>
<h2 id="给两个有序数组把第二个数组合并到第一个数组中保持有序"><a class="header" href="#给两个有序数组把第二个数组合并到第一个数组中保持有序">给两个有序数组,把第二个数组合并到第一个数组中,保持有序。</a></h2>
<pre><code class="language-java">class Solution {
public void merge(int[] nums1, int m, int[] nums2, int n) {
int i = m - 1; //从末尾开始
int j = n - 1; //从末尾开始
int k = m + n - 1; //从末尾开始
while (j &gt;= 0) {
if (i &lt; 0) {
while (j &gt;= 0) {
nums1[k--] = nums2[j--];
}
return;
}
//哪个数大就对应的添加哪个数。
if (nums1[i] &gt; nums2[j]) {
nums1[k--] = nums1[i--];
} else {
nums1[k--] = nums2[j--];
}
}
}
}
</code></pre>
<h2 id="判断是否有重复数字"><a class="header" href="#判断是否有重复数字">判断是否有重复数字。</a></h2>
<pre><code class="language-java">public boolean containsDuplicate(int[] nums) {
HashSet&lt;Integer&gt; set = new HashSet&lt;&gt;();
for (int i = 0; i &lt; nums.length; i++) {
if (set.contains(nums[i])) {
return true;
}
set.add(nums[i]);
}
return false;
}
</code></pre>
<h2 id="给一个二叉搜索树找到树中第-k-小的树二叉搜索树的定义如下"><a class="header" href="#给一个二叉搜索树找到树中第-k-小的树二叉搜索树的定义如下">给一个二叉搜索树,找到树中第 k 小的树。二叉搜索树的定义如下:</a></h2>
<p>若任意节点的左子树不空,则左子树上所有节点的值均小于它的根节点的值; 若任意节点的右子树不空,则右子树上所有节点的值均大于它的根节点的值;
任意节点的左、右子树也分别为二叉查找树; 没有键值相等的节点。</p>
<pre><code class="language-java">/**
* Definition for a binary tree node.
* public class TreeNode {
* int val;
* TreeNode left;
* TreeNode right;
* TreeNode() {}
* TreeNode(int val) { this.val = val; }
* TreeNode(int val, TreeNode left, TreeNode right) {
* this.val = val;
* this.left = left;
* this.right = right;
* }
* }
*/
int num = 0;
int res;
public int kthSmallest(TreeNode root, int k) {
inorderTraversal(root, k);
return res;
}
private void inorderTraversal(TreeNode node, int k) {
if (node == null) {
return;
}
inorderTraversal(node.left, k);
num++;
if (num == k) {
res = node.val;
return;
}
inorderTraversal(node.right, k);
}
</code></pre>
<h2 id="最长上升子序列的长度"><a class="header" href="#最长上升子序列的长度">最长上升子序列的长度。</a></h2>
<pre><code class="language-java">public int lengthOfLIS(int[] nums) {
int n = nums.length;
if (n == 0) {
return 0;
}
int dp[] = new int[n];
int len = 0;
for (int i = 0; i &lt; n; i++) {
int start = 0;
int end = len;
while (start &lt; end) {
int mid = (start + end) &gt;&gt;&gt; 1;
if (dp[mid] &lt; nums[i]) {
start = mid + 1;
} else {
end = mid;
}
}
dp[start] = nums[i];
if (start == len) {
len++;
}
}
return len;
}
</code></pre>
<h2 id="将所有的-0-移动到末尾并且保持其他数字的相对顺序不变"><a class="header" href="#将所有的-0-移动到末尾并且保持其他数字的相对顺序不变">将所有的 0 移动到末尾,并且保持其他数字的相对顺序不变。</a></h2>
<pre><code class="language-java">public void moveZeroes(int[] nums) {
int j = 0;
for (int i = 0; i &lt; nums.length; i++) {
//不等于 0 就交换
if (nums[i] != 0) {
int temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
j++;
}
}
}
</code></pre>
<h2 id="数组nums包含从0到n的所有整数但其中缺了一个请编写代码找出那个缺失的整数在on时间内完成"><a class="header" href="#数组nums包含从0到n的所有整数但其中缺了一个请编写代码找出那个缺失的整数在on时间内完成">数组nums包含从0到n的所有整数但其中缺了一个。请编写代码找出那个缺失的整数。在O(n)时间内完成</a></h2>
<pre><code class="language-javascript">```js
数组nums包含从0到n的所有整数但其中缺了一个。
请编写代码找出那个缺失的整数。在O(n)时间内完成
```;
const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let long = [];
rl.question("", function (line) {
long = line.split(",");
console.log(miss_one(long));
rl.close();
});
function miss_one(arr) {
let data = 0;
for (let i = 0; i &lt; arr.length; i++) {
//先用0异或0~nums.length的下标
data ^= i;
//再异或元素一个数字出现两次异或后为0而异或最终的结果值为出现一次的数即为缺失的元素
data ^= parseInt(arr[i]);
// console.log(data);
}
data ^= arr.length;
return data;
}
</code></pre>
<h2 id="数组求最大值"><a class="header" href="#数组求最大值">数组求最大值</a></h2>
<pre><code class="language-javascript">function getMax(arr) {
return Math.max.apply(null, arr);
}
</code></pre>
<h2 id="统计数组中的元素出现次数"><a class="header" href="#统计数组中的元素出现次数">统计数组中的元素出现次数</a></h2>
<pre><code class="language-javascript">let data = [1, 1, 2, 3, 1, 2, 45, 21];
let objGroup = data.reduce(function (obj, name) {
obj[name] = obj[name] ? ++obj[name] : 1;
return obj;
}, {});
console.log(objGroup); //{ "1": 3, "2": 2, "3": 1, "21": 1, "45": 1 }
console.log(Object.entries(objGroup)); //[ [ "1", 3 ], [ "2", 2 ], [ "3", 1 ], [ "21", 1 ], [ "45", 1 ] ]
console.log(Object.keys(objGroup)); //[ "1", "2", "3", "21", "45" ]
console.log(Object.values(objGroup)); //[ 3, 2, 1, 1, 1 ]
</code></pre>
<h2 id="已知圆中心求半径问题"><a class="header" href="#已知圆中心求半径问题">已知圆中心,求半径问题</a></h2>
<pre><code class="language-javascript">const readline = require("readline");
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
let i = 0;
var long = 0;
rl.question("", function (line) {
long = parseInt(line);
});
var x = [];
var y = [];
rl.on("line", function (line) {
i += 1;
x.push(parseInt(line.split(" ")[0]));
y.push(parseInt(line.split(" ")[1]));
if (i == long) {
console.log(main(x, y));
rl.close();
}
});
function main(_x, _y) {
// let _x = [2, -2, 3, -3, 1,2,-1];
// let _y = [-3, -3, 0, -1, -2, -2, 0];
let result = [];
for (let i = 0; i &lt; _x.length; i++) {
for (let j = 0; j &lt; _x.length; j++) {
result.push(
Math.pow(Math.pow(_x[i] - _x[j], 2) + Math.pow(_y[i] - _y[j], 2), 0.5) /
2,
);
}
}
let radius = result.filter((x) =&gt; {
return x !== 0;
}).sort();
const unique = [
...new Set(
result.filter((x) =&gt; {
return x !== 0;
}).sort(),
),
];
console.log(unique);
let objGroup = radius.reduce(function (obj, name) {
obj[name] = obj[name] ? ++obj[name] : 1;
return obj;
}, {});
console.log(Object.entries(objGroup));
function getMax(arr) {
return Math.max.apply(null, arr);
}
let max_count = getMax(Object.values(objGroup));
let max_index = Object.values(objGroup).lastIndexOf(max_count);
return Object.entries(objGroup)[max_index][0];
}
</code></pre>
<h2 id="位数相加直到和位个位数"><a class="header" href="#位数相加直到和位个位数">位数相加,直到和位个位数</a></h2>
<pre><code class="language-js">function digital_root(n) {
// ...
let data = n
.toString()
.split("")
.map((x) =&gt; {
return parseInt(x);
});
let sum = 0;
data.forEach((x) =&gt; {
sum += x;
});
return sum &lt; 9 ? sum : digital_root(sum);
}
//1+6+4=11
//1+1=2
//return 2
console.log(digital_root(164));
</code></pre>
</main>
<nav class="nav-wrapper" aria-label="Page navigation">
<!-- Mobile navigation buttons -->
<a rel="prev" href="../../posts/leetcode/swift_codewar.html" class="mobile-nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../../posts/leetcode/java_leetcode.html" class="mobile-nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
<div style="clear: both"></div>
</nav>
</div>
</div>
<nav class="nav-wide-wrapper" aria-label="Page navigation">
<a rel="prev" href="../../posts/leetcode/swift_codewar.html" class="nav-chapters previous" title="Previous chapter" aria-label="Previous chapter" aria-keyshortcuts="Left">
<i class="fa fa-angle-left"></i>
</a>
<a rel="next prefetch" href="../../posts/leetcode/java_leetcode.html" class="nav-chapters next" title="Next chapter" aria-label="Next chapter" aria-keyshortcuts="Right">
<i class="fa fa-angle-right"></i>
</a>
</nav>
</div>
<script>
window.playground_line_numbers = true;
</script>
<script>
window.playground_copyable = true;
</script>
<script src="../../ace.js"></script>
<script src="../../editor.js"></script>
<script src="../../mode-rust.js"></script>
<script src="../../theme-dawn.js"></script>
<script src="../../theme-tomorrow_night.js"></script>
<script src="../../elasticlunr.min.js"></script>
<script src="../../mark.min.js"></script>
<script src="../../searcher.js"></script>
<script src="../../clipboard.min.js"></script>
<script src="../../highlight.js"></script>
<script src="../../book.js"></script>
<!-- Custom JS scripts -->
<script src="../../src/js/custom.js"></script>
</div>
</body>
</html>