• Stars
    star
    118
  • Rank 299,923 (Top 6 %)
  • Language
    JavaScript
  • Created almost 7 years ago
  • Updated over 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

📚 ☕ This is a code example for recording algorithm questions(这是用于记录算法题的代码示例)

Brush Question

This is a code example for recording algorithm questions.

Brush question mainly from LeetCode, Codewars and some programming books, the use of C#, Java,JavaScript, Python3 and Golang to achieve programming effect.

If you have a better idea of how to solve problems, please share with us in the issues and share the code with you if you can.
I hereby express my heart-felt gratitude.

background image

Question List

No. Title Title Form Difficulty Solution
1 Remove Duplicates from Sorted Array LeetCode Easy JavaScriptGolang
2 Best Time to Buy and Sell Stock II LeetCode Easy JavaScriptGolang
3 Rotate Array LeetCode Easy JavaScriptGolang
4 Contains Duplicate LeetCode Easy JavaScript
5 Single Number LeetCode Easy JavaScript
6 Intersection of Two Arrays II LeetCode Easy JavaScript
7 Plus One LeetCode Easy JavaScript
8 Move Zeroes LeetCode Easy JavaScript
9 Two Sum LeetCode Easy JavaScript
10 Valid Sudoku LeetCode Easy JavaScript

Question Description

Remove Duplicates from Sorted Array

Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length.

Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory.

Example 1:

    Given nums = [1,1,2],

    Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively.

    It doesn't matter what you leave beyond the returned length.

Example 2:

    Given nums = [0,0,1,1,1,2,2,3,3,4],

    Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively.

    It doesn't matter what values are set beyond the returned length.

Solution: JavaScriptGolang

Best Time to Buy and Sell Stock II

Say you have an array for which the ith element is the price of a given stock on day i.

Design an algorithm to find the maximum profit. You may complete as many transactions as you like (i.e., buy one and sell one share of the stock multiple times).

Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again).

Example 1:

    Input: [7,1,5,3,6,4]
    Output: 7
    Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4.
                 Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3.

Example 2:

    Input: [1,2,3,4,5]
    Output: 4
    Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4.
                 Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are
                 engaging multiple transactions at the same time. You must sell before buying again.

Example 3:

    Input: [7,6,4,3,1]
    Output: 0
    Explanation: In this case, no transaction is done, i.e. max profit = 0.

Solution: JavaScriptGolang

Rotate Array

Given an array, rotate the array to the right by k steps, where k is non-negative.

Example 1:

    Input: [1,2,3,4,5,6,7] and k = 3
    Output: [5,6,7,1,2,3,4]
    Explanation:
        rotate 1 steps to the right: [7,1,2,3,4,5,6]
        rotate 2 steps to the right: [6,7,1,2,3,4,5]
        rotate 3 steps to the right: [5,6,7,1,2,3,4]

Example 2:

    Input: [-1,-100,3,99] and k = 2
    Output: [3,99,-1,-100]
    Explanation: 
        rotate 1 steps to the right: [99,-1,-100,3]
        rotate 2 steps to the right: [3,99,-1,-100]

Note:

  • Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem.
  • Could you do it in-place with O(1) extra space?

Solution: JavaScriptGolang

Contains Duplicate

Given an array of integers, find if the array contains any duplicates.

Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

Example 1:

    Input: [1,2,3,1]
    Output: true

Example 2:

    Input: [1,2,3,4]
    Output: false

Example 3:

    Input: [1,1,1,3,3,4,3,2,4,2]
    Output: true

Solution: JavaScript

Single Number

Given a non-empty array of integers, every element appears twice except for one. Find that single one.

Note: Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory?

Example 1:

    Input: [2,2,1]
    Output: 1

Example 2:

    Input: [4,1,2,1,2]
    Output: 4

Solution: JavaScript

Intersection of Two Arrays II

Given two arrays, write a function to compute their intersection.

Example:

    Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2].

Note:

  • Each element in the result should appear as many times as it shows in both arrays.
  • The result can be in any order.

Follow up:

  • What if the given array is already sorted? How would you optimize your algorithm?
  • What if nums1's size is small compared to nums2's size? Which algorithm is better?
  • What if elements of nums2 are stored on disk, and the memory is limited such that you cannot load all elements into the memory at once?

Solution: JavaScript

Plus One

Given a non-empty array of digits representing a non-negative integer, plus one to the integer.

The digits are stored such that the most significant digit is at the head of the list, and each element in the array contain a single digit.

You may assume the integer does not contain any leading zero, except the number 0 itself.

Example 1:

    Input: [1,2,3]
    Output: [1,2,4]
    Explanation: The array represents the integer 123.

Example 2:

    Input: [4,3,2,1]
    Output: [4,3,2,2]
    Explanation: The array represents the integer 4321.

Solution: JavaScript

Move Zeroes

Given an array nums, write a function to move all 0's to the end of it while maintaining the relative order of the non-zero elements.

Example:

   Input: [0,1,0,3,12]
   Output: [1,3,12,0,0]

Note:

  • You must do this in-place without making a copy of the array.
  • Minimize the total number of operations.

Solution: JavaScript

Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:

    Given nums = [2, 7, 11, 15], target = 9,

    Because nums[0] + nums[1] = 2 + 7 = 9,
    return [0, 1].

Solution: JavaScript

Valid Sudoku

Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules:

  1. Each row must contain the digits 1-9 without repetition.
  2. Each column must contain the digits 1-9 without repetition.
  3. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition.


A partially filled sudoku which is valid.

The Sudoku board could be partially filled, where empty cells are filled with the character '.'.

Example1:

    Input:
    [
        ["5","3",".",".","7",".",".",".","."],
        ["6",".",".","1","9","5",".",".","."],
        [".","9","8",".",".",".",".","6","."],
        ["8",".",".",".","6",".",".",".","3"],
        ["4",".",".","8",".","3",".",".","1"],
        ["7",".",".",".","2",".",".",".","6"],
        [".","6",".",".",".",".","2","8","."],
        [".",".",".","4","1","9",".",".","5"],
        [".",".",".",".","8",".",".","7","9"]
    ]
    Output: true

Example2:

    Input:
    [
        ["8","3",".",".","7",".",".",".","."],
        ["6",".",".","1","9","5",".",".","."],
        [".","9","8",".",".",".",".","6","."],
        ["8",".",".",".","6",".",".",".","3"],
        ["4",".",".","8",".","3",".",".","1"],
        ["7",".",".",".","2",".",".",".","6"],
        [".","6",".",".",".",".","2","8","."],
        [".",".",".","4","1","9",".",".","5"],
        [".",".",".",".","8",".",".","7","9"]
    ]
    Output: false
    Explanation: Same as Example 1, except with the 5 in the top left corner being 
                modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid.

Note:

  • A Sudoku board (partially filled) could be valid but is not necessarily solvable.
  • Only the filled cells need to be validated according to the mentioned rules.
  • The given board contain only digits 1-9 and the character '.'.
  • The given board size is always 9x9.

Solution: JavaScript

More Repositories

1

wechatByNode

Use the Node.js development WeChat(使用Node.js 开发微信公众号)【 博客地址:https://cnodejs.org/user/SilenceHVK 】
JavaScript
675
star
2

front-ui

😄 🎨 Collect some front-end special effects (收集一些前端特效)
JavaScript
268
star
3

blog

📚 :octocat: Github static blog post, experience the fun of using Issues.Welcome star( 静态博客文章,体验一下使用 Issues 的乐趣,欢迎 star )个人博客地址:blog.hvkcoder.me/love
Jupyter Notebook
232
star
4

learn-webpack

☕ Example code for learning Webpack.(用于学习 Webpack 的实例代码)
JavaScript
71
star
5

docker-compose-monocular-ui

📚 ☕ There are a lot of problems with installing Monocular UI with Helm. I found the Docker Compose installation method on the Internet and integrated it here.(使用 Helm 安装 Monocular UI 遇到了很多问题,在网上找到了 Docker Compose 安装的方法,在此整合一下)
Go
15
star
6

dataxs

👉🌟 Private customization based on DataX(基于 DataX 的私人订制)
Java
6
star
7

html5-games

🎮 HTML 5 game development collection, will use the CreateJs game engine and cocosJS game engine(HTML 5 游戏开发集合,会用到 CreateJs 游戏引擎 和 cocosJS 游戏引擎)
JavaScript
6
star
8

idea-maven-plugin

idea maven 打包插件
Java
3
star
9

dataxs-executor

👉🌟 Use golang to develop dataxs executor(使用 golang 开发 dataxs 执行器)
Go
2
star
10

tomcat-8.5.73

tomcat 源码学习
Java
1
star
11

silencehvk.github.io

📚 :octocat: 个人博客,博客地址:https://blog.hvkcoder.me
JavaScript
1
star
12

spring-boot-application

🔥🪛Spring boot develops scaffolding, integrating Spring Cloud, Spring Cloud Alibaba, MyBatis Plus, Alibaba Druid, and more(Spring boot 开发脚手架,整合 Spring Cloud、Spring Cloud Alibaba、MyBatis Plus、Alibaba Druid等)
Java
1
star
13

gradle-java-cli

🔥🪛 A Gradle Java project scaffolding(一个 gradle java项目脚手架)
1
star