leetcode-數組篇
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-數組篇的更多相关文章
- GO語言基礎教程:數組,切片,map
這節課我們來講解數組,切片和map,或許您是從其他語言轉到GO語言這邊的,那麼在其他語言的影響下您可能會不太適應GO語言的數組,因為GO語言把數組給拆分成了array,slice和map,接下來的時間 ...
- bzoj 1041: [HAOI2008]圆上的整点 本原勾股數組
1041: [HAOI2008]圆上的整点 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2027 Solved: 853[Submit][Stat ...
- Contest 20140914 Mushroom写情书 字符串雙hash 後綴數組
0111:Mushroom写情书 查看 提交 统计 提问 总时间限制: 10000ms 内存限制: 256000kB 描述 有一天,Mushroom准备向他的GF表白,为了增加表白成功率,Mush ...
- bzoj 1031: [JSOI2007]字符加密Cipher 後綴數組模板題
1031: [JSOI2007]字符加密Cipher Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3157 Solved: 1233[Submit ...
- js字符串轉數組,數組轉字符串
字符串轉數組:split(',') 數組轉字符串:join(‘,’) https://www.cnblogs.com/woodk/p/5714329.html
- js數組
數組對象創建: var a=new Array(); var b=new Array(1); var a=new Array(“AA“,”AA“): 相關函數: sort()排序,可以進行字面上排序s ...
- shell變量和數組
我們要知道shell是一個很重要的腳本能幫助我們完成很多事情 shell語言其實和很多的語言的語法是差不多的 變量: 變量的定義很簡單的,但是等號兩邊是不可以有空格的(不能有空格) 命名只能使用英文字 ...
- c++ LeetCode (初级字符串篇) 九道算法例题代码详解(二)
原文作者:aircraft 原文链接:https://www.cnblogs.com/DOMLX/p/11089327.html 已经刷了很多篇leetcode题了,不过最近在找c++的实习工作(大佬 ...
- LeetCode总结 -- 高精度篇
我们常见的一些主要的数据结构比方整型int或者浮点型float由于位数过多无法用内置类型存储,这时候我们就须要自己实现高精度的数据类型来进行存储和运算.这样的问题在实际产品中还是比較有用的,所以相对来 ...
- 【持续更新】leetcode算法-数组篇
会在近期陆续地完成数组篇的整理,希望对找工作的小伙伴有所帮助. 1.Two Sum:两数相加为一固定值,求其下标.一次遍历数组,用一个hash表存储已经访问过的数及其下标,对于新访问的数value ...
随机推荐
- Jquery使用ajax与Flask后端进行数据交互
最近做项目碰到一个坑,jquery使用ajax向flask传输数据的时候,在后端采用request.data无论如何都获取不到数据,代码如下: 前端: <script> function ...
- quick start guide for XMEGA ADC
This is the quick start guide for the Analog to Digital Converter (ADC), with step-by-step instructi ...
- 如何在PHP框架里把Traits使用起来
我们都知道PHP只能使用单一继承,这意味着一个类只能从另一个类继承. 例如,您可能想从几个不同的类继承方法以防止代码重复. PHP 5.4中添加了新的语言特性Traits,而且它在Laravel框架 ...
- 3、看源码MVC中的Controllr的Json方法
无论ViewResult还是JsonResult都继承ActionResult,ActionResult里只有一个方法ExecuteResult 1.Controllr的Json方法 实际上是new ...
- Spring cloud ——EurekaServer
Eureka作为服务注册与发现的组件,Eureka2.0已经闭源了,但是本教程还是以Eureka为核心进行展开. 1.三个模块 Spring Cloud Eureka是Spring Cloud Net ...
- SAP MM ME57界面看到的供应源跟Source List主数据不一致?
SAP MM ME57界面看到的供应源跟Source List主数据不一致? ME57,为如下的PR单据分配供应源, 发现找不到想要分配的供应源. ME03 去看Source List主数据, 黄色标 ...
- JavaScript图形实例:四瓣花型图案
设有坐标计算公式如下: X=L*(1+SIN(4α))*COS(α) Y=L*(1+SIN(4α))*SIN(α) 用循环依次取α值为0~2π,计算出X和Y,在canvas画布中对坐标位置(X,Y)描 ...
- Appium(十):元素定位(加强版)
1. 元素定位 写完上一篇元素定位的博客,发现实用性基本为零.这几天真的烦死我了,一直在找资料,还去看了一遍appium官网文档.最后结合着selenium的定位方法,测试出几种可行的元素定位方法. ...
- 一篇文章看懂JS闭包,都要2020年了,你怎么能还不懂闭包?
壹 ❀ 引 我觉得每一位JavaScript工作者都无法避免与闭包打交道,就算在实际开发中不使用但面试中被问及也是常态了.就我而言对于闭包的理解仅止步于一些概念,看到相关代码我知道这是个闭包,但闭包 ...
- 做了这么久的程序员,你知道为什么会有 Lambda 表达式吗?
Java8 有一些新的特性,今天老顾给大家分享一下关于 Lambda 表达式的由来,一开始感觉 Lambda 可读性蛮差的,不知道 为什么 Java8 会出来这个表达式?既然大佬们推出来,肯定是有原因 ...