first init
This commit is contained in:
41
array/1binary search/27remove_target/CMakeLists.txt
Normal file
41
array/1binary search/27remove_target/CMakeLists.txt
Normal file
@@ -0,0 +1,41 @@
|
||||
# 1. 指定CMake最低版本要求
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
# 2. 设置项目信息
|
||||
project(
|
||||
27remove_target # 项目名称
|
||||
VERSION 1.0.0 # 项目版本
|
||||
DESCRIPTION "Simple C++ Project"
|
||||
LANGUAGES CXX # 支持的语言
|
||||
)
|
||||
|
||||
# 设置C++标准
|
||||
set(CMAKE_CXX_STANDARD 17)
|
||||
set(CMAKE_CXX_STANDARD_REQUIRED ON)
|
||||
set(CMAKE_C_COMPILER "g++")
|
||||
# 源文件收集
|
||||
file(GLOB_RECURSE SOURCES "src/*.cpp") # 递归收集源文件
|
||||
file(GLOB_RECURSE HEADERS "include/*.h") # 头文件
|
||||
|
||||
# 8. 创建目标
|
||||
# 可执行文件目标
|
||||
add_executable(${PROJECT_NAME} ${SOURCES} ${HEADERS})
|
||||
|
||||
# 14. 自定义目标
|
||||
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
|
||||
# 设置输出目录
|
||||
set(EXECUTABLE_OUTPUT_PATH ${PROJECT_SOURCE_DIR}/bin)
|
||||
|
||||
# 配置调试信息(默认启用)
|
||||
if(CMAKE_BUILD_TYPE STREQUAL "Debug")
|
||||
add_compile_options(-g3 -O0)
|
||||
endif()
|
||||
|
||||
# 13.2 生成版本信息
|
||||
configure_file(
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/cmake/Version.hpp.in
|
||||
${CMAKE_CURRENT_BINARY_DIR}/Version.hpp
|
||||
)
|
||||
|
||||
|
||||
|
||||
49
array/1binary search/27remove_target/kotlin/App.kt
Normal file
49
array/1binary search/27remove_target/kotlin/App.kt
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* This Kotlin source file was generated by the Gradle 'init' task.
|
||||
*/
|
||||
package binarySearch
|
||||
|
||||
class Solution {
|
||||
fun removeElement(nums: IntArray, target: Int): Int {
|
||||
var fast_index :Int = 0
|
||||
var slow_index :Int = 0
|
||||
while (fast_index < nums.size) {
|
||||
if (nums[fast_index] != target) {
|
||||
nums[slow_index] = nums[fast_index]
|
||||
slow_index++
|
||||
}
|
||||
fast_index++
|
||||
}
|
||||
return slow_index
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
// 测试用例
|
||||
fun main() {
|
||||
val nums = intArrayOf(0, 1, 2, 3, 4, 5, 6, 7, 8)
|
||||
|
||||
println("请输入要剔除的数字:")
|
||||
val input = readLine()
|
||||
|
||||
// 验证输入有效性
|
||||
if (input.isNullOrBlank()) {
|
||||
println("错误:未输入任何内容")
|
||||
return
|
||||
}
|
||||
|
||||
val target = input.toIntOrNull() ?: run {
|
||||
println("错误:请输入有效的整数")
|
||||
return
|
||||
}
|
||||
|
||||
val solution = Solution()
|
||||
val result = solution.removeElement(nums, target )
|
||||
if (result == -1){
|
||||
println("不存在$target")
|
||||
}
|
||||
else{
|
||||
println("删除后数组长度: $result")
|
||||
}
|
||||
}
|
||||
|
||||
153
array/1binary search/27remove_target/python/27. 移除元素.ipynb
Normal file
153
array/1binary search/27remove_target/python/27. 移除元素.ipynb
Normal file
@@ -0,0 +1,153 @@
|
||||
{
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"# [27. 移除元素](https://leetcode.cn/problems/remove-element)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 题目描述 \n",
|
||||
"\n",
|
||||
"给你一个数组 `nums` 和一个值 `val`,你需要 **[原地](https://baike.baidu.com/item/%E5%8E%9F%E5%9C%B0%E7%AE%97%E6%B3%95)** 移除所有数值等于 `val` 的元素。元素的顺序可能发生改变。然后返回 `nums` 中与 `val` 不同的元素的数量。\n",
|
||||
"\n",
|
||||
"假设 `nums` 中不等于 `val` 的元素数量为 `k`,要通过此题,您需要执行以下操作:\n",
|
||||
"\n",
|
||||
"- 更改 `nums` 数组,使 `nums` 的前 `k` 个元素包含不等于 `val` 的元素。`nums` 的其余元素和 `nums` 的大小并不重要。\n",
|
||||
"- 返回 `k`。\n",
|
||||
"\n",
|
||||
"**用户评测:**\n",
|
||||
"\n",
|
||||
"评测机将使用以下代码测试您的解决方案:\n",
|
||||
"\n",
|
||||
"```txt\n",
|
||||
"int[] nums = [...]; // 输入数组\n",
|
||||
"int val = ...; // 要移除的值\n",
|
||||
"int[] expectedNums = [...]; // 长度正确的预期答案。\n",
|
||||
" // 它以不等于 val 的值排序。\n",
|
||||
"\n",
|
||||
"int k = removeElement(nums, val); // 调用你的实现\n",
|
||||
"\n",
|
||||
"assert k == expectedNums.length;\n",
|
||||
"sort(nums, 0, k); // 排序 nums 的前 k 个元素\n",
|
||||
"for (int i = 0; i < actualLength; i++) {\n",
|
||||
" assert nums[i] == expectedNums[i];\n",
|
||||
"}\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"如果所有的断言都通过,你的解决方案将会 **通过**。\n",
|
||||
"\n",
|
||||
"**示例 1:**\n",
|
||||
"\n",
|
||||
"```txt\n",
|
||||
"输入:nums = [3,2,2,3], val = 3\n",
|
||||
"输出:2, nums = [2,2,_,_]\n",
|
||||
"解释:你的函数函数应该返回 k = 2, 并且 nums 中的前两个元素均为 2。\n",
|
||||
"你在返回的 k 个元素之外留下了什么并不重要(因此它们并不计入评测)。\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"**示例 2:**\n",
|
||||
"\n",
|
||||
"```txt\n",
|
||||
"输入:nums = [0,1,2,2,3,0,4,2], val = 2\n",
|
||||
"输出:5, nums = [0,1,4,0,3,_,_,_]\n",
|
||||
"解释:你的函数应该返回 k = 5,并且 nums 中的前五个元素为 0,0,1,3,4。\n",
|
||||
"注意这五个元素可以任意顺序返回。\n",
|
||||
"你在返回的 k 个元素之外留下了什么并不重要(因此它们并不计入评测)。\n",
|
||||
"```\n",
|
||||
"\n",
|
||||
"**提示:**\n",
|
||||
"\n",
|
||||
"- `0 <= nums.length <= 100`\n",
|
||||
"- `0 <= nums[i] <= 50`\n",
|
||||
"- `0 <= val <= 100`"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"---\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"## 解答"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"class Solution(object):\n",
|
||||
" def removeElement(self, nums, val):\n",
|
||||
" \"\"\"\n",
|
||||
" :type nums: List[int]\n",
|
||||
" :type val: int\n",
|
||||
" :rtype: int\n",
|
||||
" \"\"\"\n",
|
||||
" slow_index = 0\n",
|
||||
" for fast_index in range(len(nums)):\n",
|
||||
" if nums[fast_index] != val:\n",
|
||||
" nums[slow_index] = nums[fast_index]\n",
|
||||
" slow_index += 1\n",
|
||||
" return slow_index, nums[:slow_index]"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"id": "ac31a7c1",
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"原始数组: 3223\n",
|
||||
"原始数组长度: 4\n",
|
||||
"剔除3后数组: (2, [2, 2])\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"solution = Solution()\n",
|
||||
"nums = [3,2,2,3]\n",
|
||||
"val = 3\n",
|
||||
"print('原始数组:',''.join(map(str, nums)))\n",
|
||||
"print('原始数组长度:', len(nums))\n",
|
||||
"print('剔除3后数组:', solution.removeElement(nums, val)) # Output: 2"
|
||||
]
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
"language_info": {
|
||||
"codemirror_mode": {
|
||||
"name": "ipython",
|
||||
"version": 3
|
||||
},
|
||||
"file_extension": ".py",
|
||||
"mimetype": "text/x-python",
|
||||
"name": "python",
|
||||
"nbconvert_exporter": "python",
|
||||
"pygments_lexer": "ipython3",
|
||||
"version": "3.8.10"
|
||||
}
|
||||
},
|
||||
"nbformat": 5,
|
||||
"nbformat_minor": 10
|
||||
}
|
||||
39
array/1binary search/27remove_target/src/27.cpp
Normal file
39
array/1binary search/27remove_target/src/27.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
using namespace std;
|
||||
|
||||
class Solution {
|
||||
public:
|
||||
int removeElement(vector<int>& nums, int val) {
|
||||
|
||||
int count = nums.size();
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (nums[i] != val)
|
||||
{
|
||||
nums[slow_index] = nums[i];
|
||||
slow_index++;
|
||||
}
|
||||
}
|
||||
return slow_index;
|
||||
|
||||
}
|
||||
private:
|
||||
|
||||
int slow_index = 0;
|
||||
};
|
||||
|
||||
int main()
|
||||
{
|
||||
vector<int> nums = {0, 1, 2, 2, 3, 0, 4, 2};
|
||||
int val = 2;
|
||||
Solution solution;
|
||||
int result = solution.removeElement(nums, val);
|
||||
cout << "The length of nums after removing val is: " << result << endl;
|
||||
for (int i = 0; i < result; i++)
|
||||
{
|
||||
cout << nums[i] << " ";
|
||||
}
|
||||
system("pause");
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user