Remove Element

public class Lc27 {
public static int removeElement(int[] nums, int val) { if (nums == null || nums.length == 0) {
return 0;
} int count = 0;
for (int i = 0; i < nums.length;) {
if (nums[i] == val) {
count++;
move(nums, i);
} else {
i++;
}
}
return nums.length - count;
} public static void move(int[] nums, int position) {
for (int i = position; i < nums.length - 1; i++) {
nums[i] = nums[i + 1];
}
nums[nums.length - 1] = Integer.MAX_VALUE;
} public static void main(String[] args) {
int[] nums = { 0, 1, 2, 2, 3, 0, 4, 2 };
int val = 2;
System.out.println(removeElement(nums, val));
}
}

Remove Duplicates from Sorted Array,这里用了六种排序,由于题目中有负数导致基数排序可能会出现数组下标错误

import java.util.ArrayList;
import java.util.List; /**
* 俩个排序算法
*
*/
public class Lc26 { public static int removeDuplicates(int[] nums) {
int count = 0;
for (int i = 0; i < nums.length - 1; i++) {
if (nums[i] == nums[i + 1]) {
nums[i] = Integer.MAX_VALUE;
count++;
}
}
// nums = insertSort(nums);
// nums = shellSort(nums);
// nums = selectSort(nums);
// nums = bubbleSort(nums);
// quickSort(nums, 0, nums.length - 1);
nums = radixSort(nums); return nums.length - count;
} /**
* 插入排序,遍历所有的元素和以前排好的元素,如果选择的元素比以前的元素小就替换。
*/
public static int[] insertSort(int[] nums) {
for (int i = 1; i < nums.length; i++) {
for (int j = 0; j < i; j++) {
if (nums[i] < nums[j]) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
break;
}
}
}
return nums;
} /**
* 插入排序,遍历所有的元素和以前排好的元素,如果选择的元素比以前的元素小就替换。 希尔排序:在插入排序的基础上增加控制增量,逻辑分组数据,每次比较俩端数据
*/
public static int[] shellSort(int[] nums) {
for (int gap = nums.length / 2; gap > 0; gap /= 2) {
for (int i = gap; i < nums.length; i++) {
for (int j = i; j >= gap; j -= gap) {
if (nums[j] < nums[j - gap]) {
int temp = nums[j];
nums[j] = nums[j - gap];
nums[j - gap] = temp;
}
}
}
}
return nums;
} /**
* 选择排序:再要排序中的数组中,选出最小的数字和第一个数字替换,一次选出第二小的数组和第二个数字替换,一次类推
*/
public static int[] selectSort(int[] nums) {
for (int i = 0; i < nums.length; i++) {
int index = nums[i];
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] < nums[i]) {
int temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
}
}
}
return nums;
} /*
* 冒泡排序:对当前还未排好顺序的元素进行自顶向下排序,交换相邻的俩个数字,小数上浮,大数下沉
*/
public static int[] bubbleSort(int[] nums) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[j] < nums[i]) {
int temp = nums[j];
nums[j] = nums[i];
nums[i] = temp;
}
}
}
return nums;
} /**
* 快速排序:选取一个基准值,利用二分法对其排序
*/
public static void quickSort(int[] nums, int low, int hign) {
if (low < hign) {
int index = getIndex(nums, low, hign);
quickSort(nums, 0, index - 1);
quickSort(nums, index + 1, hign);
}
} private static int getIndex(int[] nums, int low, int hign) {
int temp = nums[low];
while (low < hign) {
while (low < hign && nums[hign] >= temp) {
hign--;
}
nums[low] = nums[hign]; while (low < hign && nums[low] <= temp) {
low++;
}
nums[hign] = nums[low];
}
nums[low] = temp;
return low;
} /**
* 基数排序:低位优先,比较每个数字的低位,依次到高位,注意是正整数
*/
public static int[] radixSort(int[] nums) {
if (nums == null || nums.length == 0) {
return nums;
}
int max = nums[0];
for (int i = 0; i < nums.length; i++) {
if (max < nums[i]) {
max = nums[i];
}
} int digit = 0;
while (max != 0) {
max /= 10;
digit++;
} // init list
List<List<Integer>> buckets = new ArrayList<List<Integer>>();
for (int i = 0; i < 10; i++) {
buckets.add(new ArrayList<>());
} for (int i = 0; i < digit; i++) {
for (int j = 0; j < nums.length; j++) {
int key = (int) (nums[j] % Math.pow(10, i + 1) / Math.pow(10, i));
buckets.get(key).add(nums[j]);
}
int count = 0;
for (int j = 0; j < nums.length; j++) { while (buckets.get(j).size() > 0) {
nums[count++] = buckets.get(j).remove(0);
}
}
// 分配完之后,将桶中的元素依次复制回数组
}
return nums;
} public static void main(String[] args) {
int[] nums = { -1, 0, 0, 0, 0, 3, 3 };
System.out.println(removeDuplicates(nums));
} }
 

Remove Duplicates from Sorted Array II

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map; public class Lc80 {
public static int removeDuplicates(int[] nums) { // map(数字/次数)
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
int count = 0;
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
int temp = map.get(nums[i]);
if (temp >= 2) {
count++;
nums[i] = Integer.MAX_VALUE;
} else {
map.put(nums[i], ++temp);
}
} else {
map.put(nums[i], 1);
}
} Arrays.sort(nums);
return nums.length - count;
} public static void main(String[] args) {
int[] nums = { 1, 1, 1 };
System.out.println(removeDuplicates(nums));
}
}
 

Find the Celebrity

 

Rotate Array

public class Lc189 {
public static void rotate(int[] nums, int k) {
int previous = 0;
for (int i = 0; i < k; i++) {
previous = nums[nums.length - 1];
for (int j = 0; j < nums.length; j++) {
int temp = previous;
previous = nums[j];
nums[j] = temp;
}
}
} public static void main(String[] args) {
int[] nums = { 1, 2, 3, 4, 5, 6, 7 };
int k = 3;
rotate(nums, k);
} }
 

First Missing Positive

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map; public class Lc41 {
public static int firstMissingPositive(int[] nums) {
Map<Integer, Integer> map = new HashMap<Integer, Integer>();
for (int i = 0; i < nums.length; i++) {
if (map.containsKey(nums[i])) {
nums[i] = Integer.MAX_VALUE;
} else {
map.put(nums[i], i);
}
} Arrays.sort(nums);
int min = 0;
int minPosition = 0;
for (int i = 0; i < nums.length; i++) {
if (nums[i] > 0) {
minPosition = i;
min = nums[i];
break;
}
}
if (min != 1) {
return 1;
} int j = 1;
for (int i = minPosition; i < nums.length; i++, j++) {
if (nums[i] != j) {
return j;
}
}
return j; } public static void move(int[] nums, int position) {
for (int i = 0; i < nums.length - 1; i++) {
nums[i] = nums[i + 1];
}
nums[nums.length - 1] = Integer.MAX_VALUE;
} public static void main(String[] args) {
int[] nums = { 0, 2, 2, 1, 1 };
System.out.println(firstMissingPositive(nums));
}
}
 

Bulls and Cows

import java.util.ArrayList;
import java.util.List; public class Lc229 {
public static String getHint(String secret, String guess) {
int bulls = 0;
int cows = 0;
List<String> secretDigits = convertToAscall(secret);
List<String> guessDigits = convertToAscall(guess);
for (int i = 0; i < secretDigits.size(); i++) {
if (secretDigits.get(i).equals(guessDigits.get(i))) {
bulls++;
} else {
cows++;
}
}
return bulls + "A" + cows + "B"; } private static List<String> convertToAscall(String s) {
StringBuffer sb = new StringBuffer();
char[] chars = s.toCharArray();
for (char c : chars) {
sb.append((int) c).append(",");
}
String[] str = sb.toString().split(",");
List<String> lists = new ArrayList<>();
for (String string : str) {
lists.add(string);
} return lists;
} public static void main(String[] args) {
String secret = "1123";
String guess = "0111";
System.out.println(getHint(secret, guess));
}
}

参考文档:https://cspiration.com/leetcodeClassification#10309

 

leetcode-數組篇的更多相关文章

  1. GO語言基礎教程:數組,切片,map

    這節課我們來講解數組,切片和map,或許您是從其他語言轉到GO語言這邊的,那麼在其他語言的影響下您可能會不太適應GO語言的數組,因為GO語言把數組給拆分成了array,slice和map,接下來的時間 ...

  2. bzoj 1041: [HAOI2008]圆上的整点 本原勾股數組

    1041: [HAOI2008]圆上的整点 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2027  Solved: 853[Submit][Stat ...

  3. Contest 20140914 Mushroom写情书 字符串雙hash 後綴數組

    0111:Mushroom写情书 查看 提交 统计 提问 总时间限制:  10000ms 内存限制:  256000kB 描述 有一天,Mushroom准备向他的GF表白,为了增加表白成功率,Mush ...

  4. bzoj 1031: [JSOI2007]字符加密Cipher 後綴數組模板題

    1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3157  Solved: 1233[Submit ...

  5. js字符串轉數組,數組轉字符串

    字符串轉數組:split(',') 數組轉字符串:join(‘,’) https://www.cnblogs.com/woodk/p/5714329.html

  6. js數組

    數組對象創建: var a=new Array(); var b=new Array(1); var a=new Array(“AA“,”AA“): 相關函數: sort()排序,可以進行字面上排序s ...

  7. shell變量和數組

    我們要知道shell是一個很重要的腳本能幫助我們完成很多事情 shell語言其實和很多的語言的語法是差不多的 變量: 變量的定義很簡單的,但是等號兩邊是不可以有空格的(不能有空格) 命名只能使用英文字 ...

  8. c++ LeetCode (初级字符串篇) 九道算法例题代码详解(二)

    原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/11089327.html 已经刷了很多篇leetcode题了,不过最近在找c++的实习工作(大佬 ...

  9. LeetCode总结 -- 高精度篇

    我们常见的一些主要的数据结构比方整型int或者浮点型float由于位数过多无法用内置类型存储,这时候我们就须要自己实现高精度的数据类型来进行存储和运算.这样的问题在实际产品中还是比較有用的,所以相对来 ...

  10. 【持续更新】leetcode算法-数组篇

    会在近期陆续地完成数组篇的整理,希望对找工作的小伙伴有所帮助.   1.Two Sum:两数相加为一固定值,求其下标.一次遍历数组,用一个hash表存储已经访问过的数及其下标,对于新访问的数value ...

随机推荐

  1. 在VS2017中连接到SQLite数据源(dbfist)

    在VS2017中配置.连接到SQLite数据源(dbfist) 需要安装的VS插件 SQLite/SQL Server Compact ToolBox 这个插件安装后,在选择数据源时已经可以选择SQL ...

  2. 【Java Web开发学习】Spring加载外部properties配置文件

    [Java Web开发学习]Spring加载外部properties配置文件 转载:https://www.cnblogs.com/yangchongxing/p/9136505.html 1.声明属 ...

  3. 最新115道华为、京东、滴滴、美团精选Java面试题整理

    京东面试题 1. 一般sql注入怎么发现触点的,从源码阐述sqlmap如何测试注入点的. 2. masscan扫描端口时靠什么检测,为什么这么快? 请详述. 3. 你写过哪些小工具,你为你使用过的工具 ...

  4. Linux之find命令操作技巧

    一.前言 工作中,我们常用find命令查找某些文件或者删除一些旧的日志文件,所以学会find命令的操作技巧就极为重要. 二.find常用参数选项 -depth 从指定目录下最深层的子目录开始查找 -m ...

  5. Docker容器监控

    利用docker compose组合应用并利用scale可以快速对容器进行扩充,而docker compose启动的服务容器都在同一台宿主机上,对于一个宿主机上运行多个容器应用时,容器的运行情况,如: ...

  6. spring源码学习(四)-spring生命周期用到的后置处理器

    生命周期的九大后置处理器 第一次调用后置处理器org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory# ...

  7. HttpRunner学习9--切换测试报告模板

    前言 在HttpRunner中,给我们提供了 2 套测试报告模板,分别是 default_report_template.html 和 extent_report_template.html . 默认 ...

  8. [ASP.NET Core 3框架揭秘] 文件系统[3]:物理文件系统

    ASP.NET Core应用中使用得最多的还是具体的物理文件,比如配置文件.View文件以及作为Web资源的静态文件.物理文件系统由定义在NuGet包"Microsoft.Extension ...

  9. Sql: Oracle paging

    --书分类目录kind --涂聚文 Geovin Du create table geovindu.BookKindList ( BookKindID INT PRIMARY KEY, BookKin ...

  10. FontLab VI for Mac 键盘快捷键

    使用FontLab VI for Mac,您可以创建,打开,修改,绘制,空间,文字,提示和导出桌面,网页,颜色和可变字体.该应用程序是一个全能的字体编辑器,但也支持与其他字体创建工具的数据交换,使其易 ...