├── golang ├── maxProfit.go ├── rotateArray.go └── removeDuplicates.go ├── javascript ├── singleNumber.js ├── rotateArray.js ├── removeDuplicates.js ├── maxProfit.js ├── twoSum.js ├── plusOne.js ├── intersect.js ├── containsDuplicate.js ├── moveZeroes.js └── isValidSudoku.js ├── README-CN.md └── README.md /golang/maxProfit.go: -------------------------------------------------------------------------------- 1 | package solution 2 | 3 | func maxProfit(prices []int) int { 4 | result := 0 5 | length := len(prices) - 1 6 | for i, v := range prices { 7 | if i != length && v < prices[i + 1] { 8 | result += prices[i + 1] - v 9 | } 10 | } 11 | return result 12 | } -------------------------------------------------------------------------------- /golang/rotateArray.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | func rotate(nums []int, k int) { 4 | length := len(nums) 5 | for i := 0; i < k; i++ { 6 | for j := 0; j < length; j++ { 7 | nums[length - 1], nums[j] = nums[j], nums[length - 1] 8 | } 9 | } 10 | } -------------------------------------------------------------------------------- /javascript/singleNumber.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * @param {number[]} nums 5 | * @return {number} 6 | */ 7 | var singleNumber = function (nums) { 8 | let result = 0; 9 | for (let i = 0; i < nums.length; i++) { 10 | result ^= nums[i]; 11 | } 12 | return result; 13 | }; -------------------------------------------------------------------------------- /golang/removeDuplicates.go: -------------------------------------------------------------------------------- 1 | package solution 2 | 3 | func removeDuplicates(nums []int) int { 4 | length := len(nums) 5 | if length <= 1 { 6 | return length 7 | } 8 | 9 | length = 1; 10 | for i , v := range nums { 11 | if i > 0 && nums[length - 1] != v { 12 | nums[length] = v 13 | length++ 14 | } 15 | } 16 | return length 17 | } -------------------------------------------------------------------------------- /javascript/rotateArray.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * @param {number[]} nums 5 | * @param {number} k 6 | * @return {void} Do not return anything, modify nums in-place instead. 7 | */ 8 | var rotate1 = function (nums, k) { 9 | for (let i = 0; i < k; i++) { 10 | const lastItem = nums.pop(); 11 | nums.unshift(lastItem); 12 | } 13 | }; -------------------------------------------------------------------------------- /javascript/removeDuplicates.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * @param {number[]} nums 5 | * @return {number} 6 | */ 7 | var removeDuplicates = function (nums) { 8 | for (let i = nums.length; i >= 0; i--) { 9 | const temp = nums.slice(i + 1); 10 | if (temp.includes(nums[i])) { 11 | nums.splice(i, 1); 12 | } 13 | } 14 | return nums.length; 15 | } -------------------------------------------------------------------------------- /javascript/maxProfit.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * @param {number[]} prices 5 | * @return {number} 6 | */ 7 | var maxProfit = function (prices) { 8 | let result = 0; 9 | for (let i = 0; i < prices.length; i++) { 10 | if (prices[i] < prices[i + 1]) { 11 | result += prices[i + 1] - prices[i]; 12 | } 13 | } 14 | return result < 0 ? 0 : result; 15 | }; -------------------------------------------------------------------------------- /javascript/twoSum.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * @param {number[]} nums 5 | * @param {number} target 6 | * @return {number[]} 7 | */ 8 | var twoSum = function (nums, target) { 9 | for (let i = 0; i < nums.length; i++) { 10 | const temp = target - nums[i]; 11 | if (nums.indexOf(temp) > -1 && nums.indexOf(temp) != i) { 12 | return [i, nums.indexOf(temp)]; 13 | } 14 | } 15 | }; -------------------------------------------------------------------------------- /javascript/plusOne.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * @param {number[]} digits 5 | * @return {number[]} 6 | */ 7 | var plusOne = function (digits) { 8 | for (let i = digits.length - 1; i >= 0; i--) { 9 | if (digits[i] === 9) { 10 | digits[i] = 0; 11 | } else { 12 | digits[i] += 1; 13 | return digits; 14 | } 15 | } 16 | if (digits[0] === 0) { 17 | digits.unshift(1); 18 | } 19 | return digits; 20 | }; -------------------------------------------------------------------------------- /javascript/intersect.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * @param {number[]} nums1 5 | * @param {number[]} nums2 6 | * @return {number[]} 7 | */ 8 | var intersect = function (nums1, nums2) { 9 | const result = []; 10 | const temp = nums2.slice(0); 11 | for (let i = 0; i < nums1.length; i++) { 12 | const item = nums1[i]; 13 | if (temp.includes(nums1[i])) { 14 | result.push(item); 15 | temp.splice(temp.indexOf(item), 1); 16 | } 17 | } 18 | return result; 19 | }; -------------------------------------------------------------------------------- /javascript/containsDuplicate.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * ES5 Solution 5 | * @param {number[]} nums 6 | * @return {boolean} 7 | */ 8 | var containsDuplicate = function (nums) { 9 | var temp = nums.filter(function (item, index) { 10 | return nums.indexOf(item) === index; 11 | }); 12 | return !(temp.length === nums.length); 13 | }; 14 | 15 | 16 | /** 17 | * ES6 Solution 18 | * @param {number[]} nums 19 | * @return {boolean} 20 | */ 21 | var containsDuplicate = function (nums) { 22 | const temp = [...new Set(nums)]; 23 | return !(temp.length === nums.length); 24 | }; -------------------------------------------------------------------------------- /javascript/moveZeroes.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * @param {number[]} nums 5 | * @return {void} Do not return anything, modify nums in-place instead. 6 | */ 7 | var moveZeroes1 = function (nums) { 8 | for (let i = nums.length - 1; i >= 0; i--) { 9 | if (nums[i] === 0) { 10 | nums.splice(i, 1); 11 | nums.push(0); 12 | } 13 | } 14 | }; 15 | 16 | /** 17 | * @param {number[]} nums 18 | * @return {void} Do not return anything, modify nums in-place instead. 19 | */ 20 | var moveZeroes = function (nums) { 21 | let y = 0; 22 | for (let i = 0; i < nums.length; i++) { 23 | if (nums[i] !== 0) { 24 | [nums[i], nums[y]] = [nums[y], nums[i]]; 25 | y++; 26 | } 27 | } 28 | }; -------------------------------------------------------------------------------- /javascript/isValidSudoku.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | /** 4 | * @param {character[][]} board 5 | * @return {boolean} 6 | */ 7 | var isValidSudoku = function (board) { 8 | console.log(board); 9 | for (let i = 0; i < board.length; i++) { 10 | for (let j = 0; j < board.length; j++) { 11 | const rowItem = board[i][j]; 12 | console.log(rowItem); 13 | } 14 | } 15 | return true; 16 | }; 17 | 18 | const sudoku = [ 19 | ["5", "3", ".", ".", "7", ".", ".", ".", "."], 20 | ["6", ".", ".", "1", "9", "5", ".", ".", "."], 21 | [".", "9", "8", ".", ".", ".", ".", "6", "."], 22 | ["8", ".", ".", ".", "6", ".", ".", ".", "3"], 23 | ["4", ".", ".", "8", ".", "3", ".", ".", "1"], 24 | ["7", ".", ".", ".", "2", ".", ".", ".", "6"], 25 | [".", "6", ".", ".", ".", ".", "2", "8", "."], 26 | [".", ".", ".", "4", "1", "9", ".", ".", "5"], 27 | [".", ".", ".", ".", "8", ".", ".", "7", "9"] 28 | ]; 29 | 30 | console.log(isValidSudoku(sudoku)); -------------------------------------------------------------------------------- /README-CN.md: -------------------------------------------------------------------------------- 1 | # Brush Question 2 | 3 | - [英文(English)](https://github.com/SilenceHVK/brush-question/blob/master/README.md) 4 | 5 |   这是用于记录算法题的代码示例。 6 | 7 |   Brush Question 的题目主要来自 LeetCode,Codewars 和一些编程书籍,使用 C#,Java,JavaScript,Python3 和 Golang 来实现编程效果。 8 | 9 |   如果你有更好的解决方案,如果可以的话,请在 issues 分享你的代码。 10 | 11 |   我在此表示衷心的感谢。 12 | 13 | 14 | ![背景图片](https://github.com/SilenceHVK/Articles/raw/master/assets/images/bgImages/bg3.png) 15 | 16 | # 问题列表 17 | 18 | | 序号 | 题目 | 题目出处 | 难易度 | 解决方案 | 19 | | ---|---|---|---|--- | 20 | | 1 | [从排序数组中删除重复项](#user-content-从排序数组中删除重复项) | [LeetCode](https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/21/) | 简单 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/removeDuplicates.js) [Golang](https://github.com/SilenceHVK/brush-question/blob/master/golang/removeDuplicates.go) | 21 | | 2 | [买卖股票的最佳时机 II](#user-content-买卖股票的最佳时机-ii) | [LeetCode](https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/22/) | 简单 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/maxProfit.js) [Golang](https://github.com/SilenceHVK/brush-question/blob/master/golang/maxProfit.go) | 22 | | 3 | [旋转数组](#user-content-旋转数组) | [LeetCode](https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/23/) | 简单 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/rotateArray.js) [Golang](https://github.com/SilenceHVK/brush-question/blob/master/golang/rotateArray.go)| 23 | | 4 | [存在重复](#user-content-存在重复) | [LeetCode](https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/24/) | 简单 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/containsDuplicate.js)| 24 | | 5 | [只出现一次的数字](#user-content-只出现一次的数字) | [LeetCode](https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/25/) | 简单 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/singleNumber.js)| 25 | | 6 | [两个数组的交集 II](#user-content-两个数组的交集-ii) | [LeetCode](https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/26/) | 简单 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/intersect.js)| 26 | | 7 | [加一](#user-content-加一) | [LeetCode](https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/27/) | 简单 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/plusOne.js)| 27 | | 8 | [移动零](#user-content-移动零) | [LeetCode](https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/28/) | 简单 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/moveZeroes.js)| 28 | | 9 | [两数之和](#user-content-两数之和) | [LeetCode](https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/29/) | 简单 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/twoSum.js)| 29 | | 10 | [有效的数独](#user-content-有效的数独) | [LeetCode](https://leetcode-cn.com/explore/interview/card/top-interview-questions-easy/1/array/30/) | 简单 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/isValidSudoku.js)| 30 | 31 | # 问题描述 32 | 33 | ## 从排序数组中删除重复项 34 | 35 |   给定一个排序数组,你需要在原地删除重复出现的元素,使得每个元素只出现一次,返回移除后数组的新长度。 36 | 37 |   不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成。 38 | 39 | 示例 1: 40 | ``` 41 | 给定 nums = [0,0,1,1,1,2,2,3,3,4], 42 | 43 | 函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。 44 | 45 | 你不需要考虑数组中超出新长度后面的元素。 46 | ``` 47 | 48 | 示例 2: 49 | ``` 50 | 给定 nums = [0,0,1,1,1,2,2,3,3,4], 51 | 52 | 函数应该返回新的长度 5, 并且原数组 nums 的前五个元素被修改为 0, 1, 2, 3, 4。 53 | 54 | 你不需要考虑数组中超出新长度后面的元素。 55 | 56 | ``` 57 | 58 | 解决方案: 59 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/removeDuplicates.js)  60 | [Go](https://github.com/SilenceHVK/brush-question/blob/master/golang/removeDuplicates.go) 61 | 62 | ## 买卖股票的最佳时机 II 63 | 64 | 给定一个数组,它的第 i 个元素是一支给定股票第 i 天的价格。 65 | 66 | 设计一个算法来计算你所能获取的最大利润。你可以尽可能地完成更多的交易(多次买卖一支股票)。 67 | 68 | 注意:你不能同时参与多笔交易(你必须在再次购买前出售掉之前的股票)。 69 | 70 | 示例 1: 71 | ``` 72 | 输入: [7,1,5,3,6,4] 73 | 输出: 7 74 | 解释: 在第 2 天(股票价格 = 1)的时候买入,在第 3 天(股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。 75 | 随后,在第 4 天(股票价格 = 3)的时候买入,在第 5 天(股票价格 = 6)的时候卖出, 这笔交易所能获得利润 = 6-3 = 3 。 76 | ``` 77 | 78 | 示例 2: 79 | ``` 80 | 输入: [1,2,3,4,5] 81 | 输出: 4 82 | 解释: 在第 1 天(股票价格 = 1)的时候买入,在第 5 天 (股票价格 = 5)的时候卖出, 这笔交易所能获得利润 = 5-1 = 4 。 83 | 注意你不能在第 1 天和第 2 天接连购买股票,之后再将它们卖出。 84 | 因为这样属于同时参与了多笔交易,你必须在再次购买前出售掉之前的股票。 85 | ``` 86 | 87 | 示例 3: 88 | ``` 89 | 输入: [7,6,4,3,1] 90 | 输出: 0 91 | 解释: 在这种情况下, 没有交易完成, 所以最大利润为 0。 92 | ``` 93 | 94 | 解决方案: 95 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/maxProfit.js)  96 | [Go](https://github.com/SilenceHVK/brush-question/blob/master/golang/maxProfit.go) 97 | 98 | ## 旋转数组 99 | 100 | 给定一个数组,将数组中的元素向右移动 k 个位置,其中 k 是非负数。 101 | 102 | 示例 1: 103 | ``` 104 | 输入: [1,2,3,4,5,6,7] 和 k = 3 105 | 输出: [5,6,7,1,2,3,4] 106 | 解释: 107 | 向右旋转 1 步: [7,1,2,3,4,5,6] 108 | 向右旋转 2 步: [6,7,1,2,3,4,5] 109 | 向右旋转 3 步: [5,6,7,1,2,3,4] 110 | ``` 111 | 112 | 示例 2: 113 | ``` 114 | 输入: [1,2,3,4,5,6,7] 和 k = 3 115 | 输出: [5,6,7,1,2,3,4] 116 | 解释: 117 | 输入: [-1,-100,3,99] 和 k = 2 118 | 输出: [3,99,-1,-100] 119 | 解释: 120 | 向右旋转 1 步: [99,-1,-100,3] 121 | 向右旋转 2 步: [3,99,-1,-100] 122 | ``` 123 | 124 | 说明: 125 | - 尽可能想出更多的解决方案,至少有三种不同的方法可以解决这个问题。 126 | - 要求使用空间复杂度为 O(1) 的原地算法。 127 | 128 | 解决方案: 129 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/rotateArray.js) [Golang](https://github.com/SilenceHVK/brush-question/blob/master/golang/rotateArray.go) 130 | 131 | ## 存在重复 132 | 133 | 给定一个整数数组,判断是否存在重复元素。 134 | 135 | 如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。 136 | 137 | 示例 1: 138 | ``` 139 | 输入: [1,2,3,1] 140 | 输出: true 141 | ``` 142 | 143 | 示例 2: 144 | ``` 145 | 输入: [1,2,3,4] 146 | 输出: false 147 | ``` 148 | 149 | 示例 3: 150 | ``` 151 | 输入: [1,1,1,3,3,4,3,2,4,2] 152 | 输出: true 153 | ``` 154 | 155 | 解决方案: 156 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/containsDuplicate.js) 157 | 158 | ## 只出现一次的数字 159 | 160 | 给定一个非空整数数组,除了某个元素只出现一次以外,其余每个元素均出现两次。找出那个只出现了一次的元素。 161 | 162 | 说明: 163 | 你的算法应该具有线性时间复杂度。 你可以不使用额外空间来实现吗? 164 | 165 | 示例 1: 166 | ``` 167 | 输入: [2,2,1] 168 | 输出: 1 169 | ``` 170 | 171 | 示例 2: 172 | ``` 173 | 输入: [4,1,2,1,2] 174 | 输出: 4 175 | ``` 176 | 177 | 解决方案: 178 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/singleNumber.js) 179 | 180 | ## 两个数组的交集 II 181 | 182 | 给定两个数组,写一个方法来计算它们的交集。 183 | 184 | 示例 1: 185 | ``` 186 | 给定 nums1 = [1, 2, 2, 1], nums2 = [2, 2], 返回 [2, 2]. 187 | ``` 188 | 189 | 注意: 190 | - 输出结果中每个元素出现的次数,应与元素在两个数组中出现的次数一致。 191 | - 我们可以不考虑输出结果的顺序。 192 | 193 | 跟进: 194 | - 如果给定的数组已经排好序呢?你将如何优化你的算法? 195 | - 如果 nums1 的大小比 nums2 小很多,哪种方法更优? 196 | - 如果nums2的元素存储在磁盘上,内存是有限的,你不能一次加载所有的元素到内存中,你该怎么办? 197 | 198 | 解决方案: 199 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/intersect.js) 200 | 201 | ## 加一 202 | 203 | 给定一个非负整数组成的非空数组,在该数的基础上加一,返回一个新的数组。 204 | 205 | 最高位数字存放在数组的首位, 数组中每个元素只存储一个数字。 206 | 207 | 你可以假设除了整数 0 之外,这个整数不会以零开头。 208 | 209 | 示例 1: 210 | ``` 211 | 输入: [1,2,3] 212 | 输出: [1,2,4] 213 | 解释: 输入数组表示数字 123。 214 | ``` 215 | 216 | 示例 2: 217 | ``` 218 | 输入: [4,3,2,1] 219 | 输出: [4,3,2,2] 220 | 解释: 输入数组表示数字 4321。 221 | ``` 222 | 223 | 解决方案: 224 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/plusOne.js) 225 | 226 | ## 移动零 227 | 228 | 给定一个数组 nums,编写一个函数将所有 0 移动到数组的末尾,同时保持非零元素的相对顺序。 229 | 230 | 示例: 231 | ``` 232 | 输入: [0,1,0,3,12] 233 | 输出: [1,3,12,0,0] 234 | ``` 235 | 236 | 说明: 237 | - 必须在原数组上操作,不能拷贝额外的数组。 238 | - 尽量减少操作次数。 239 | 240 | 241 | 解决方案: 242 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/moveZeroes.js) 243 | 244 | ## 两数之和 245 | 246 | 给定一个整数数组和一个目标值,找出数组中和为目标值的两个数。 247 | 248 | 你可以假设每个输入只对应一种答案,且同样的元素不能被重复利用。 249 | 250 | 示例: 251 | ``` 252 | 给定 nums = [2, 7, 11, 15], target = 9 253 | 254 | 因为 nums[0] + nums[1] = 2 + 7 = 9 255 | 所以返回 [0, 1] 256 | ``` 257 | 258 | 解决方案: 259 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/twoSum.js) 260 | 261 | ## 有效的数独 262 | 263 | 判断一个 9x9 的数独是否有效。只需要根据以下规则,验证已经填入的数字是否有效即可。 264 | 265 | 1. 数字 1-9 在每一行只能出现一次。 266 | 2. 数字 1-9 在每一列只能出现一次。 267 | 3. 数字 1-9 在每一个以粗实线分隔的 3x3 宫内只能出现一次。 268 | 269 |

270 |
271 | 上图是一个部分填充的有效的数独。 272 |

273 | 274 | 数独部分空格内已填入了数字,空白格用 ```'.'``` 表示。 275 | 276 | 示例1: 277 | ``` 278 | 输入: 279 | [ 280 | ["5","3",".",".","7",".",".",".","."], 281 | ["6",".",".","1","9","5",".",".","."], 282 | [".","9","8",".",".",".",".","6","."], 283 | ["8",".",".",".","6",".",".",".","3"], 284 | ["4",".",".","8",".","3",".",".","1"], 285 | ["7",".",".",".","2",".",".",".","6"], 286 | [".","6",".",".",".",".","2","8","."], 287 | [".",".",".","4","1","9",".",".","5"], 288 | [".",".",".",".","8",".",".","7","9"] 289 | ] 290 | 输出: true 291 | ``` 292 | 293 | 示例2: 294 | ``` 295 | 输入: 296 | [ 297 | ["8","3",".",".","7",".",".",".","."], 298 | ["6",".",".","1","9","5",".",".","."], 299 | [".","9","8",".",".",".",".","6","."], 300 | ["8",".",".",".","6",".",".",".","3"], 301 | ["4",".",".","8",".","3",".",".","1"], 302 | ["7",".",".",".","2",".",".",".","6"], 303 | [".","6",".",".",".",".","2","8","."], 304 | [".",".",".","4","1","9",".",".","5"], 305 | [".",".",".",".","8",".",".","7","9"] 306 | ] 307 | 输出: false 308 | 解释: 除了第一行的第一个数字从 5 改为 8 以外,空格内其他数字均与 示例1 相同。 309 | 但由于位于左上角的 3x3 宫内有两个 8 存在, 因此这个数独是无效的。 310 | ``` 311 | 312 | 说明: 313 | - 一个有效的数独(部分已被填充)不一定是可解的。 314 | - 只需要根据以上规则,验证已经填入的数字是否有效即可。 315 | - 给定数独序列只包含数字 1-9 和字符 ```'.'``` 。 316 | - 给定数独永远是 9x9 形式的。 317 | 318 | 解决方案: 319 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/isValidSudoku.js) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Brush Question 2 | 3 | - [中文(Chinese)](https://github.com/SilenceHVK/brush-question/blob/master/README-CN.md) 4 | 5 | This is a code example for recording algorithm questions. 6 | 7 | Brush question mainly from LeetCode, Codewars and some programming books, the use of C#, Java,JavaScript, Python3 and Golang to achieve programming effect. 8 | 9 | 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. 10 | I hereby express my heart-felt gratitude. 11 | 12 | ![background image](https://github.com/SilenceHVK/Articles/raw/master/assets/images/bgImages/bg3.png) 13 | 14 | # Question List 15 | | No. | Title | Title Form | Difficulty | Solution | 16 | | ---|---|---|---|--- | 17 | | 1 | [Remove Duplicates from Sorted Array](#user-content-remove-duplicates-from-sorted-array) | [LeetCode](https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/727/) | Easy | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/removeDuplicates.js) [Golang](https://github.com/SilenceHVK/brush-question/blob/master/golang/removeDuplicates.go) | 18 | | 2 | [Best Time to Buy and Sell Stock II](#user-content-best-time-to-buy-and-sell-stock-ii) | [LeetCode](https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/564/) | Easy | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/maxProfit.js) [Golang](https://github.com/SilenceHVK/brush-question/blob/master/golang/maxProfit.go) | 19 | | 3 | [Rotate Array](#user-content-rotate-array) | [LeetCode](https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/646/) | Easy | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/rotateArray.js) [Golang](https://github.com/SilenceHVK/brush-question/blob/master/golang/rotateArray.go) | 20 | | 4 | [Contains Duplicate](#user-content-contains-duplicate) | [LeetCode](https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/578/) | Easy | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/containsDuplicate.js)| 21 | | 5 | [Single Number](#user-content-single-number) | [LeetCode](https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/549/) | Easy | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/singleNumber.js)| 22 | | 6 | [Intersection of Two Arrays II](#user-content-intersection-of-two-arrays-ii) | [LeetCode](https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/674/) | Easy | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/intersect.js)| 23 | | 7 | [Plus One](#user-content-plus-one) | [LeetCode](https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/559/) | Easy | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/plusOne.js)| 24 | | 8 | [Move Zeroes](#user-content-move-zeroes) | [LeetCode](https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/567/) | Easy | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/moveZeroes.js)| 25 | | 9 | [Two Sum](#user-content-two-sum) | [LeetCode](https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/546/) | Easy | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/twoSum.js)| 26 | | 10 | [Valid Sudoku](#user-content-valid-sudoku) | [LeetCode](https://leetcode.com/explore/interview/card/top-interview-questions-easy/92/array/769/) | Easy | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/isValidSudoku.js)| 27 | 28 | 29 | 30 | # Question Description 31 | 32 | ## Remove Duplicates from Sorted Array 33 | 34 | Given a sorted array nums, remove the duplicates in-place such that each element appear only once and return the new length. 35 | 36 | Do not allocate extra space for another array, you must do this by modifying the input array in-place with O(1) extra memory. 37 | 38 | Example 1: 39 | ``` 40 | Given nums = [1,1,2], 41 | 42 | Your function should return length = 2, with the first two elements of nums being 1 and 2 respectively. 43 | 44 | It doesn't matter what you leave beyond the returned length. 45 | ``` 46 | 47 | Example 2: 48 | ``` 49 | Given nums = [0,0,1,1,1,2,2,3,3,4], 50 | 51 | Your function should return length = 5, with the first five elements of nums being modified to 0, 1, 2, 3, and 4 respectively. 52 | 53 | It doesn't matter what values are set beyond the returned length. 54 | ``` 55 | 56 | Solution: 57 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/removeDuplicates.js)  58 | [Golang](https://github.com/SilenceHVK/brush-question/blob/master/golang/removeDuplicates.go) 59 | 60 | ## Best Time to Buy and Sell Stock II 61 | 62 | Say you have an array for which the ith element is the price of a given stock on day i. 63 | 64 | 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). 65 | 66 | Note: You may not engage in multiple transactions at the same time (i.e., you must sell the stock before you buy again). 67 | 68 | Example 1: 69 | ``` 70 | Input: [7,1,5,3,6,4] 71 | Output: 7 72 | Explanation: Buy on day 2 (price = 1) and sell on day 3 (price = 5), profit = 5-1 = 4. 73 | Then buy on day 4 (price = 3) and sell on day 5 (price = 6), profit = 6-3 = 3. 74 | ``` 75 | 76 | Example 2: 77 | ``` 78 | Input: [1,2,3,4,5] 79 | Output: 4 80 | Explanation: Buy on day 1 (price = 1) and sell on day 5 (price = 5), profit = 5-1 = 4. 81 | Note that you cannot buy on day 1, buy on day 2 and sell them later, as you are 82 | engaging multiple transactions at the same time. You must sell before buying again. 83 | ``` 84 | 85 | Example 3: 86 | ``` 87 | Input: [7,6,4,3,1] 88 | Output: 0 89 | Explanation: In this case, no transaction is done, i.e. max profit = 0. 90 | ``` 91 | 92 | Solution: 93 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/maxProfit.js)  94 | [Golang](https://github.com/SilenceHVK/brush-question/blob/master/golang/maxProfit.go) 95 | 96 | ## Rotate Array 97 | 98 | Given an array, rotate the array to the right by k steps, where k is non-negative. 99 | 100 | Example 1: 101 | ``` 102 | Input: [1,2,3,4,5,6,7] and k = 3 103 | Output: [5,6,7,1,2,3,4] 104 | Explanation: 105 | rotate 1 steps to the right: [7,1,2,3,4,5,6] 106 | rotate 2 steps to the right: [6,7,1,2,3,4,5] 107 | rotate 3 steps to the right: [5,6,7,1,2,3,4] 108 | ``` 109 | 110 | Example 2: 111 | ``` 112 | Input: [-1,-100,3,99] and k = 2 113 | Output: [3,99,-1,-100] 114 | Explanation: 115 | rotate 1 steps to the right: [99,-1,-100,3] 116 | rotate 2 steps to the right: [3,99,-1,-100] 117 | ``` 118 | 119 | Note: 120 | - Try to come up as many solutions as you can, there are at least 3 different ways to solve this problem. 121 | - Could you do it in-place with O(1) extra space? 122 | 123 | Solution: 124 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/rotateArray.js)  125 | [Golang](https://github.com/SilenceHVK/brush-question/blob/master/golang/rotateArray.go) 126 | 127 | ## Contains Duplicate 128 | 129 | Given an array of integers, find if the array contains any duplicates. 130 | 131 | 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. 132 | 133 | Example 1: 134 | ``` 135 | Input: [1,2,3,1] 136 | Output: true 137 | ``` 138 | 139 | Example 2: 140 | ``` 141 | Input: [1,2,3,4] 142 | Output: false 143 | ``` 144 | 145 | Example 3: 146 | ``` 147 | Input: [1,1,1,3,3,4,3,2,4,2] 148 | Output: true 149 | ``` 150 | 151 | Solution: 152 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/containsDuplicate.js) 153 | 154 | ## Single Number 155 | 156 | Given a non-empty array of integers, every element appears twice except for one. Find that single one. 157 | 158 | Note: 159 | Your algorithm should have a linear runtime complexity. Could you implement it without using extra memory? 160 | 161 | Example 1: 162 | ``` 163 | Input: [2,2,1] 164 | Output: 1 165 | ``` 166 | 167 | Example 2: 168 | ``` 169 | Input: [4,1,2,1,2] 170 | Output: 4 171 | ``` 172 | 173 | Solution: 174 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/singleNumber.js) 175 | 176 | ## Intersection of Two Arrays II 177 | 178 | Given two arrays, write a function to compute their intersection. 179 | 180 | Example: 181 | ``` 182 | Given nums1 = [1, 2, 2, 1], nums2 = [2, 2], return [2, 2]. 183 | ``` 184 | 185 | Note: 186 | - Each element in the result should appear as many times as it shows in both arrays. 187 | - The result can be in any order. 188 | 189 | Follow up: 190 | - What if the given array is already sorted? How would you optimize your algorithm? 191 | - What if nums1's size is small compared to nums2's size? Which algorithm is better? 192 | - 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? 193 | 194 | Solution: 195 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/intersect.js) 196 | 197 | ## Plus One 198 | 199 | Given a non-empty array of digits representing a non-negative integer, plus one to the integer. 200 | 201 | 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. 202 | 203 | You may assume the integer does not contain any leading zero, except the number 0 itself. 204 | 205 | Example 1: 206 | ``` 207 | Input: [1,2,3] 208 | Output: [1,2,4] 209 | Explanation: The array represents the integer 123. 210 | ``` 211 | 212 | Example 2: 213 | ``` 214 | Input: [4,3,2,1] 215 | Output: [4,3,2,2] 216 | Explanation: The array represents the integer 4321. 217 | ``` 218 | Solution: 219 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/plusOne.js) 220 | 221 | ## Move Zeroes 222 | 223 | 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. 224 | 225 | Example: 226 | ``` 227 | Input: [0,1,0,3,12] 228 | Output: [1,3,12,0,0] 229 | ``` 230 | 231 | Note: 232 | - You must do this in-place without making a copy of the array. 233 | - Minimize the total number of operations. 234 | 235 | Solution: 236 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/moveZeroes.js) 237 | 238 | ## Two Sum 239 | 240 | Given an array of integers, return indices of the two numbers such that they add up to a specific target. 241 | 242 | You may assume that each input would have exactly one solution, and you may not use the same element twice. 243 | 244 | Example: 245 | ``` 246 | Given nums = [2, 7, 11, 15], target = 9, 247 | 248 | Because nums[0] + nums[1] = 2 + 7 = 9, 249 | return [0, 1]. 250 | ``` 251 | 252 | Solution: 253 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/twoSum.js) 254 | 255 | ## Valid Sudoku 256 | 257 | Determine if a 9x9 Sudoku board is valid. Only the filled cells need to be validated according to the following rules: 258 | 259 | 1. Each row must contain the digits 1-9 without repetition. 260 | 2. Each column must contain the digits 1-9 without repetition. 261 | 3. Each of the 9 3x3 sub-boxes of the grid must contain the digits 1-9 without repetition. 262 | 263 |

264 |
265 | A partially filled sudoku which is valid. 266 |

267 | 268 | The Sudoku board could be partially filled, where empty cells are filled with the character ```'.'```. 269 | 270 | Example1: 271 | ``` 272 | Input: 273 | [ 274 | ["5","3",".",".","7",".",".",".","."], 275 | ["6",".",".","1","9","5",".",".","."], 276 | [".","9","8",".",".",".",".","6","."], 277 | ["8",".",".",".","6",".",".",".","3"], 278 | ["4",".",".","8",".","3",".",".","1"], 279 | ["7",".",".",".","2",".",".",".","6"], 280 | [".","6",".",".",".",".","2","8","."], 281 | [".",".",".","4","1","9",".",".","5"], 282 | [".",".",".",".","8",".",".","7","9"] 283 | ] 284 | Output: true 285 | ``` 286 | 287 | Example2: 288 | ``` 289 | Input: 290 | [ 291 | ["8","3",".",".","7",".",".",".","."], 292 | ["6",".",".","1","9","5",".",".","."], 293 | [".","9","8",".",".",".",".","6","."], 294 | ["8",".",".",".","6",".",".",".","3"], 295 | ["4",".",".","8",".","3",".",".","1"], 296 | ["7",".",".",".","2",".",".",".","6"], 297 | [".","6",".",".",".",".","2","8","."], 298 | [".",".",".","4","1","9",".",".","5"], 299 | [".",".",".",".","8",".",".","7","9"] 300 | ] 301 | Output: false 302 | Explanation: Same as Example 1, except with the 5 in the top left corner being 303 | modified to 8. Since there are two 8's in the top left 3x3 sub-box, it is invalid. 304 | ``` 305 | 306 | Note: 307 | - A Sudoku board (partially filled) could be valid but is not necessarily solvable. 308 | - Only the filled cells need to be validated according to the mentioned rules. 309 | - The given board contain only digits 1-9 and the character ```'.'```. 310 | - The given board size is always 9x9. 311 | 312 | Solution: 313 | [JavaScript](https://github.com/SilenceHVK/brush-question/blob/master/javascript/isValidSudoku.js) --------------------------------------------------------------------------------