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. 聊聊JS的二进制家族:Blob、ArrayBuffer和Buffer

    事实上,前端很少涉及对二进制数据的处理,但即便如此,我们偶尔总能在角落里看见它们的身影. 今天我们就来聊一聊前端的二进制家族:Blob.ArrayBuffer和Buffer 概述 Blob: 前端的一 ...

  2. Orleans 初接触(二) 测试用例

    [返回导航] 在简单了解了Orleans 之后我们可以通过几个例子去加深印象 一.快速入门示例 这个例子也是跟着<Microsoft Orleans 之 入门指南>(https://www ...

  3. 【Spring Boot】定时任务

    [Spring Boot]定时任务 测试用业务Service package com.example.schedule.service; import org.springframework.ster ...

  4. 【Element UI】使用问题记录

    [Element UI]使用问题记录 转载:https://www.cnblogs.com/yangchongxing/p/10750994.html 下载地址: https://unpkg.com/ ...

  5. 【NPM】使用学习

    [NPM]使用学习 转载: 目录 ============================================== 1.修改 npm 模块的安装路径 2.淘宝 NPM 镜像 3.vue-c ...

  6. 为什么要使用MQ消息中间件?这3个点让你彻底明白!

    前言 一个用消息队列的人,不知道为啥用,有点尴尬.没有复习这点,很容易被问蒙,然后就开始胡扯了. 回答:这个问题,咱只答三个最主要的应用场景,不可否认还有其他的,但是只答三个主要的,即以下六个字: 解 ...

  7. webpack前期了解

    webpack的核心概念(四个) 入口(entry) 输出(output) loader 插件(plugins) Entry(入口)——指示 webpack 应该使用哪个模块,来作为构建其内部依赖图的 ...

  8. 安装lispbox出现:error while loading shared libraries: libjpeg.so.62: cannot open shared object file: No such file or directory

    如下图,运行lispbox出现如下报错(第一个命令): 检索之后发现解决办法:Solution to libjpeg.so.62 输入一下两条命令(第二条也必须执行): sudo apt-get up ...

  9. 【Python进阶】来谈谈几个常用的内置函数

    匿名函数(lambda表达式) 在Python中,函数可以算的上是“一等公民”了,我们先回顾下函数的优点: 减少代码重复量 模块化代码 但是我们有没有想过,如果我们需要一个函数,比较简短,而且只需要使 ...

  10. Winform中实现向窗体中拖放照片并显示以及拖放文件夹显示树形结构(附代码下载)

    场景 向窗体中拖拽照片并显示效果 向窗体中拖拽文件夹并显示树形结构效果 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 霸道的程序猿 ...