当前位置:网站首页>Some basic method records of commonly used languages in LeetCode
Some basic method records of commonly used languages in LeetCode
2022-08-05 06:44:00 【monkeyhlj】
文章目录
LeetCodeSome basic methods are documented in commonly used languages
注:Language is a bit confusing,所以梳理一下.
Java
数组
数组就是一种可以存储大量数据类型相同的变量的数据结构,数组就是一个具有相同数据类型的数据集合.
数组中的数据必须是同一种数据类型的.
数组的基本要素:
数组名称、数组元素、元素下标、数据类型
数组本身就是一个变量,数组名称就是变量名,数组中保存的每一个数据都会有一个下标(从 0 开始)
//声明数组
int[] array;
//分配内存空间
array = new int[6];
//给数组赋值
array[0] = 1;
array[1] = 2;
array[2] = 3;
array[3] = 4;
array[4] = 5;
array[5] = 6;
int[] array2 = {
1,2,3,4,5,6};
int[] array3 = new int[]{
1,2,3,4,5,6};
数组的常用操作及方法
- 求数组的最大值
- 求数组的最小值
- 在数组的指定位置插入数据
- 对数组进行排序 : https://blog.csdn.net/hhhmonkey/article/details/107983663
int arr = {
73,80,62,93,96,87};
int arr2 = {
73,80,62,93,96,87};
int arr3 = {
66,80,62,22};
System.out.println(arr.length); //数组长度
Arrays.equals(arr,arr1); //true
Arrays.sort(arr);
Arrays.toString(arr);
Arrays.fill(arr2,66); //填充
arr4 = Arrays.copyOf(arr3,10);
int index = Arrays.binarySearch(arr,87);
获取数组最大值
public static void main(String[] args) {
int[] nums={
1,2,3,4,5,6,7};
int max = Arrays.stream(nums).max().getAsInt();
System.out.println(max);
}
参考:https://blog.csdn.net/issunmingzhi/article/details/106413031
Arrays工具类
打印数组:
int[] intArray = {
1, 2, 3, 4, 5 };
String intArrayString = Arrays.toString(intArray);
// 直接打印,则会打印出引用对象的Hash值
// [[email protected]
System.out.println(intArray);
// [1, 2, 3, 4, 5]
System.out.println(intArrayString);
根据数组创建ArrayList:
String[] stringArray = {
"a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
// [a, b, c, d, e]
System.out.println(arrayList);
检查数组是否包含某个值:
String[] stringArray = {
"a", "b", "c", "d", "e" };
boolean b = Arrays.asList(stringArray).contains("a");
// true
System.out.println(b);
合并连接两个数组:
int[] intArray = {
1, 2, 3, 4, 5 };
int[] intArray2 = {
6, 7, 8, 9, 10 };
// Apache Commons Lang 库
int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2);
声明内联数组:
method(new String[]{
"a", "b", "c", "d", "e"});
用给定的字符串连结(join)数组:
// containing the provided list of elements
// Apache common lang
String j = StringUtils.join(new String[] {
"a", "b", "c" }, ", ");
// a, b, c
System.out.println(j);
将ArrayList转换为数组:
String[] stringArray = {
"a", "b", "c", "d", "e" };
ArrayList<String> arrayList = new ArrayList<String>(Arrays.asList(stringArray));
String[] stringArr = new String[arrayList.size()];
arrayList.toArray(stringArr);
for (String s : stringArr)
System.out.println(s);
将数组转换为Set:
Set<String> set = new HashSet<String>(Arrays.asList(stringArray));
//[d, e, b, c, a]
System.out.println(set);
数组元素反转:
int[] intArray = {
1, 2, 3, 4, 5 };
ArrayUtils.reverse(intArray);
//[5, 4, 3, 2, 1]
System.out.println(Arrays.toString(intArray));
移除元素:
int[] intArray = {
1, 2, 3, 4, 5 };
int[] removed = ArrayUtils.removeElement(intArray, 3);//创建新的数组
System.out.println(Arrays.toString(removed));
fill方法:
publicstaticvoidmain(String[] args) {
inta[]=newint[5];
//fill填充数组
Arrays.fill(a,1);
for(inti=0;i<5;i++)//输出5个1
System.out.println(a[i]);
}
publicstaticvoidmain(String[] args) {
inta[]=newint[5];
//fill填充数组
Arrays.fill(a,1,2,1);
for(inti=0;i<5;i++)//a[1]=1,其余默认为0
System.out.println(a[i]);
}
sort方法:
publicstaticvoidmain(String[] args) {
inta[]={
2,4,1,3,7};
Arrays.sort(a);
for(inti=0;i<5;i++)//升序
System.out.println(a[i]);
}
publicstaticvoidmain(String[] args) {
inta[]={
2,4,1,3,7};
Arrays.sort(a,1,4); //输出2,1,3,4,7
for(inti=0;i<5;i++)
System.out.println(a[i]);
}
Arrays.sort(intervals, new Comparator<int[]>() {
//新知识 -- 二维数组排序
public int compare(int[] a, int[] b) {
if(a[0] == b[0]) {
return a[1] - b[1];
}
return a[0] - b[0];
}
});
//见:https://blog.csdn.net/hhhmonkey/article/details/119648150
equals方法:
publicstaticvoidmain(String[] args) {
inta[]={
2,4,1,3,7};
inta1[]={
2,4,1,5,7};
System.out.println(Arrays.equals(a1, a)); //输出false
}
binarySearch方法:
publicstaticvoidmain(String[] args) {
inta[]={
2,4,1,3,7};
Arrays.sort(a);//先排序
System.out.println(Arrays.binarySearch(a, 4));//二分查找,输出3
}
copyof方法:
publicclassArrayDemo {
publicstaticvoidmain(String[] args) {
int[] arr1 = {
1, 2, 3, 4, 5};
int[] arr2 = Arrays.copyOf(arr1, arr1.length);
for(inti = 0; i < arr2.length; i++)
System.out.print(arr2[i] + " ");
System.out.println();
}
}
copyOfRange方法:
int[] pre_left = Arrays.copyOfRange(preorder,1,line+1);
Collections类常用方法总结
1、sort(Collection)方法的使用(含义:对集合进行排序).
例:对已知集合c进行排序?
public class Practice {
public static void main(String[] args){
List c = new ArrayList();
c.add("l");
c.add("o");
c.add("v");
c.add("e");
System.out.println(c);
Collections.sort(c);
System.out.println(c);
}
}
运行结果为:[l, o, v, e]
[e, l, o, v]
2.reverse()方法的使用(含义:反转集合中元素的顺序).
例:
public class Practice {
public static void main(String[] args){
List list = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(list);
Collections.reverse(list);
System.out.println(list);
}
}
运行结果为:
[one, two, three, four, five, six, siven]
[siven, six, five, four, three, two, one]
3.shuffle(Collection)方法的使用(含义:对集合进行随机排序).
例:shuffle(Collection)的简单示例?
public class Practice {
public static void main(String[] args){
List c = new ArrayList();
c.add("l");
c.add("o");
c.add("v");
c.add("e");
System.out.println(c);
Collections.shuffle(c);
System.out.println(c);
Collections.shuffle(c);
System.out.println(c);
}
}
运行结果为:[l, o, v, e]
[l, v, e, o]
[o, v, e, l]
4.fill(List list,Object o)方法的使用(含义:用对象o替换集合list中的所有元素)
例:
public class Practice {
public static void main(String[] args){
List m = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(m);
Collections.fill(m, "青鸟52T25小龙");
System.out.println(m);
}
}
运行结果为:
[one, two, three, four, five, six, siven]
[青鸟52T25小龙, 青鸟52T25小龙, 青鸟52T25小龙, 青鸟52T25小龙, 青鸟52T25小龙, 青鸟52T25小龙, 青鸟52T25小龙]
5.copy(List m,List n)方法的使用(含义:将集合n中的元素全部复制到m中,并且覆盖相应索引的元素).
例:
public class Practice {
public static void main(String[] args){
List m = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(m);
List n = Arrays.asList("我 是 复制过来的哈".split(" "));
System.out.println(n);
Collections.copy(m,n);
System.out.println(m);
}
}
运行结果为:[one, two, three, four, five, six, siven]
[我, 是, 复制过来的哈]
[我, 是, 复制过来的哈, four, five, six, siven]
6.min(Collection),min(Collection,Comparator)方法的使用(前者采用Collection内含自然比较法,后者采用Comparator进行比较).
public static void main(String[] args){
List c = new ArrayList();
c.add("l");
c.add("o");
c.add("v");
c.add("e");
System.out.println(c);
System.out.println(Collections.min(c));
}
运行结果:[l, o, v, e]
e
7.max(Collection),max(Collection,Comparator)方法的使用(前者采用Collection内含自然比较法,后者采用Comparator进行比较).
public static void main(String[] args){
List c = new ArrayList();
c.add("l");
c.add("o");
c.add("v");
c.add("e");
System.out.println(c);
System.out.println(Collections.max(c));
}
运行结果:[l, o, v, e]
v
8.indexOfSubList(List list,List subList)方法的使用(含义:查找subList在list中首次出现位置的索引).
public static void main(String[] args){
ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 6, 6, 7, 3));
ArrayList<Integer> targetList = new ArrayList<>(Arrays.asList(6));
System.out.println(Collections.indexOfSubList(intList, targetList));
}
运行结果:5
9.lastIndexOfSubList(List source,List target)The usage of the method is the same as the usage of the method in the previous example
public static void main(String[] args){
ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5, 6, 6, 6, 7, 3));
ArrayList<Integer> targetList = new ArrayList<>(Arrays.asList(6));
System.out.println(Collections.lastIndexOfSubList(intList, targetList));
}
运行结果:7
10.rotate(List list,int m)方法的使用(含义:集合中的元素向后移m个位置,在后面被遮盖的元素循环到前面来).移动列表中的元素,负数向左移动,正数向右移动
public static void main(String[] args){
ArrayList<Integer> intList = new ArrayList<>(Arrays.asList(1, 2, 3, 4, 5));
System.out.println(intList);
Collections.rotate(intList, 1);
System.out.println(intList);
}
运行结果:[1, 2, 3, 4, 5]
[5, 1, 2, 3, 4]
11.swap(List list,int i,int j)方法的使用(含义:交换集合中指定元素索引的位置)
例:
public class Practice {
public static void main(String[] args){
List m = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(m);
Collections.swap(m, 2, 3);
System.out.println(m);
}
}
运行结果为:
[one, two, three, four, five, six, siven]
[one, two, four, three, five, six, siven]
12.binarySearch(Collection,Object)方法的使用(含义:查找指定集合中的元素,返回所查找元素的索引).
例:binarySearch(Collection,Object)的简单示例?
public class Practice {
public static void main(String[] args){
List c = new ArrayList();
c.add("l");
c.add("o");
c.add("v");
c.add("e");
System.out.println(c);
int m = Collections.binarySearch(c, "o");
System.out.println(m);
}
}
运行结果为:[l, o, v, e]
1
13.replaceAll(List list,Object old,Object new)方法的使用(含义:替换批定元素为某元素,若要替换的值存在刚返回true,反之返回false).
例:
public class Practice {
public static void main(String[] args){
List list = Arrays.asList("one two three four five six siven".split(" "));
System.out.println(list);
List subList = Arrays.asList("three four five six".split(" "));
System.out.println(Collections.replaceAll(list, "siven", "siven eight"));
System.out.println(list);
}
}
运行结果为:
[one, two, three, four, five, six, siven]
true
[one, two, three, four, five, six, siven eight]
【参考链接】https://www.cnblogs.com/guweiwei/p/6511974.html
二维数组
二维数组简单理解即一维数组中保存的值是另外一个一维数组.
变量、数据类型、流程控制、循环、数组.
int[][] arr;
arr = new int[2][3];
arr[0][0] = 50;
int[][] arr2 = {
{
50,60},{
55,11,22},{
66,33,35}}; //边声明边定义
字符串常用属性及方法
参考:https://www.jianshu.com/p/7242679062d9
https://blog.csdn.net/weixin_30446613/article/details/96616981
- String commonly used properties
string.length()————>返回字符串的长度,int类型.
- 字符串常用的方法
String.contains(";")——————>判断String里面是否包含;号.返回boolen类型
String.split(";")——————————>根据";"号来分割String,返回的是字符串数组
String.indexOf(";")——————————>查找";"在string出现的位置.没出现返回-1,Occurrence returns the subscript of the occurrence
String.subString(int a)————————————>如果是一个参数,从string[a]开始截取到最后
String.subString(int a,int b)—————————————————>从string[a]截取到string[b].含a不包含b
String.toUpperCase()——————————>转大写
String.toLowerCase()————————————>转小写
String.endsWith(".txt")——————————>判断string是否是以.txt结尾
String.startsWith("a")————————————>判断string是否以a开头
String.charAt(int a)————————————>string[a]
String.compareTo("String")————————————>忽略大小写,然后做比较.返回的是boolen类型
String.replace('h','w');——————————>string中所有的h都替换成w
String.replaceFirst('h','w')————————>string第一个h替换成w
String.trim() ——————————————————> 去掉起始和结尾的空格
String.toCharArray()————————————>把string转成char类型数组
JavaScript
Common properties and methods of arrays
//使用 JavaScript 关键词 new
var color= new Array("red", "blue", "green");
//使用数组文本创建
var color2 = ["red", "blue", "green"];
//数组中的方法:
//toString() 把数组转换为数组值(逗号分隔)的字符串.
var arr1 = ["red", "blue", "green"];
arr1.toString(); //"red,blue,green"
//join() 方法也可将所有数组元素结合为一个字符串 可以定义分隔符
var arr2 = ["red", "blue", "green"];
console.log(arr2.join(":")) //red:blue:green
console.log(arr2.join(" ")) //red blue green
//添加新元素
//unshift() 方法(在开头)向数组添加新元素
var arr3 = ["red", "blue", "green"];
arr3.unshift("abc");//["abc", "red", "blue", "green"]
//push() 方法(在数组结尾处)向数组添加一个新的元素
arr3.push("xyz"); //["abc", "red", "blue", "green","xyz"]
//删除元素
//pop() 方法从数组中删除最后一个元素 pop()Returns the value that was popped
var arr4 = ["red", "blue", "green"];
arr4.pop(); //"green" arr4 ======["red", "blue"]
//shift() 方法会删除首个数组元素,并把所有其他元素“位移”到更低的索引 Returns the value that was popped
arr4.shift();//"red"
//修改元素
//splice() 方法可删除从 index 处开始的零个或多个元素,并且用参数列表中声明的一个或多个值来替换那些被删除的元素.如果从 arrayObject 中删除了元素,则返回的是含有被删除的元素的数组
var arr5 = [5,6,7,8];
// arr5.splice(位置,删除的个数,添加的新元素)
arr5.splice(1,1); //删除了6 arr5 的结果是[5,7,8]
arr5.splice(1,0,9);//arr5 的结果是[5, 9, 6, 7, 8]
arr5.splice(1,2,3);//arr5 的结果是 [5, 3, 8]
//slice()返回一个新的数组,包含从 start 到 end (不包括该end元素)的 arrayObject 中的元素
var arr6 = [5,6,7,8];
arr6.slice(1,3) //[6, 7]
//arr6 仍然还是[5,6,7,8]; Returns the selected element,该方法不会修改原数组
//合并数组
//concat() 方法用于连接两个或多个数组 This method does not change the original array
var arr7 =[1,2,3];
var arr8 = [4,5,6];
var arr9 = arr7.concat(arr8);
//arr9 [1, 2, 3, 4, 5, 6]
//sort()排序
var arr10 =["red", "blue", "green"];
arr10.sort(); // ["blue", "green", "red"]
var arr11 = [1,10,5,12,4,9,22];
arr11.sort();// [1, 10, 12, 22, 4, 5, 9]Sorted by the first number
//reverse() 方法用于颠倒数组中元素的顺序 会改变原数组
arr11.reverse();// [22, 9, 4, 12, 5, 10, 1]
//遍历数组
//for循环遍历
var arr12 =["red", "blue", "green"];
var str = "";
for(var i = 0;i < arr12.length;i++){
str += "<p>"+arr12[i]+"</p>";
};
//forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数.
var arr13 =[1,2,3];
var a14=[];
arr13.forEach(function(item,index){
a14.push(item+1); //2 3 4
});
//筛选数组
//filter()对数组的每一项都运行给定的函数,返回 结果为 ture 的项组成的数组
var arr15 = [2,3,5,6,7,18,9];
var a15 = arr15.filter(function(item,index){
return item > 5; //满足的条件 大于5的结果[6, 7, 18, 9]
});
//every()对数组的每一项都运行给定的函数,每一项都返回 ture,则返回 true
//Returns when each item satisfies the conditiontrue,Just one dissatisfaction foot returnfalse
var a16 = arr15.every(function(item,index){
//return item >5; //Every number must be requested>5才会返回true 否则返回false
return item>1; //true
});
//some() Just one full foot returntrue,都不满 foot returnfalse
var a17 = arr15.some(function(item,index){
//return item >5; //Just one full 足了条件,返回true
return item==0; //false 都不满足 返回false
});
//reduce()Beggars beg for food(Similar to accumulation)
var arr15 = [2,3,5];
var a18 = arr15.reduce(function(total,cur){
return total+cur
},10); //10表示从total初始值是10,从10开始累加
console.log(a18) //20
字符串常用属性及方法
var str = 'hello wrold';
var str1 = 'monkey ';
//属性 截取字符串的长度
document.write(str.length); //11
//charAt() 方法可返回指定位置的字符
document.write(str.charAt(1)); //e
document.write(str.charAt(str.length-1)); //d 获取最后一个字符
//concat() 方法用于连接两个或多个字符串
var s = str1.concat(str,' welcome'); //monkey hello world welcome
//indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置.区分大小写
document.write(str.indexOf('o')); //4 Returns the index value after a successful match
document.write(str.indexOf('a')); //-1 Returns if there is no match-1
document.write(str.indexOf('o',5)); //8 indexOf(查找的值,开始的位置)
//lastIndexOf() 方法可返回一个指定的字符串值最后出现的位置
document.write(str.lastIndexOf('o')); //8
document.write(str.lastIndexOf('o',5)); //4
//replace() 方法用于在字符串中用一些字符替换另一些字符,或替换一个与正则表达式匹配的子串
//replace(searchValue,newValue) 返回的是新的字符串
var s = str.replace('hello','hi,'); //hi,world
//split() 方法用于把一个字符串分割成字符串数组
var str3 = 'how,are,you';
document.write(str3.split(",")); // ["how", "are", "you"]
document.write(str3.split(",",2)); // ["how", "are"] 2表示返回数组的最大长度
//substr() method extracts a specified number of characters from the string starting at the start index
document.write(str.substr(4)); //o wrold
document.write(str.substr(2,4));//substr(start,length) "llo "
//substring() 方法用于提取字符串中介于两个指定下标之间的字符
document.write(str.substring(4)); //o wrold
document.write(str.substring(2,4)); //substr(from,to) ll 不包括to
//slice(start, end) 方法可提取字符串的某个部分,并以新的字符串返回被提取的部分
document.write(str.slice(2,4)); //ll
document.write(str.slice(-1)); //d -1表示最后一个字符串
document.write(str.substring(-1)); //-1 表示0 hello world
//slice()和substring()区别 思考题
/* var str="abcdefghijkl"; console.log(str.slice(3,-4)); //defgh console.log(str.substring(3,-4)); //abc*/
//toLowerCase() 方法用于把字符串转换为小写
//toUpperCase() 方法用于把字符串转换为大写
//trim() 方法用于删除字符串的头尾空格
var str5 = ' hello ';
document.write(str5.trim()); //hello
https://blog.csdn.net/hhhmonkey/article/details/118601657
https://blog.csdn.net/aphy358/article/details/49904643
创建二维数组
var dp = new Array(n).fill(0).map(()=>new Array(n).fill(0));
Python
Commonly used properties and methods in arrays
参考:https://blog.csdn.net/qq_36134318/article/details/80729949
Create a regular two-dimensional list
[[0 for col in range(cols)] for row in range(rows)]
dp = [[0] * n for i in range(n)]
[x for x in arr if x>5 ] #筛选
[x if x>5 else x+100 for x in arr ]
[x if z else y for x,y,z in zip(arr1,arr2,condition)] #和np.where函数一样的功能
字典
创建字典:
dict01 = {
'name1':'joe','name2':'suan','name3':'anne'}
#访问:
dict01['name']
#修改或添加:
dict01['address'] = '泰国' #The same key cannot appear twice,Otherwise the latter overrides the former
#删除:
del dict01['sex']
字符串常用方法
参考:https://blog.csdn.net/weixin_43158056/article/details/92798114
边栏推荐
- Dry!Teach you to use industrial raspberries pie combining CODESYS configuration EtherCAT master station
- What should I do if the SSL certificate prompts that it is expired or invalid?
- document.querySelector()方法
- 干货!教您使用工业树莓派结合CODESYS配置EtherCAT主站
- H5开发调试-Fiddler手机抓包
- [问题已处理]-jenkins流水线checkout超时
- 七种让盒子水平垂直居中的方法
- BIO,NIO,AIO实践学习笔记(便于理解理论)
- Mina's long and short connections
- Nacos集群的搭建过程详解
猜你喜欢
Tencent Internal Technology: Evolution of Server Architecture of "The Legend of Xuanyuan"
Autoware--Beike Tianhui rfans lidar uses the camera & lidar joint calibration file to verify the fusion effect of point cloud images
DevOps流程demo(实操记录)
Successful indie developers deal with failure & imposters
The use of three parameters of ref, out, and Params in Unity3D
Vim tutorial: vimtutor
Collision, character controller, Cloth components (cloth), joints in the Unity physics engine
BIO, NIO, AIO practical study notes (easy to understand theory)
selenium learning
Configuration of routers and static routes
随机推荐
初识网页与浏览器
el-autocomplete使用
Nacos配置服务的源码解析(全)
Mina断线重连
One-arm routing experiment and three-layer switch experiment
Disk management and file systems
Native JS takes you to understand the implementation and use of array methods
Successful indie developers deal with failure & imposters
干货!教您使用工业树莓派结合CODESYS配置EtherCAT主站
Q 2020, the latest senior interview Laya soul, do you know?
GetEnumerator method and MoveNext and Reset methods in Unity
从“双卡双待“到”双通“,vivo率先推动DSDA架构落地
The size of the screen adaptation
Four ways to obtain Class objects through reflection
Quick question and quick answer - FAQ of Tencent Cloud Server
系统基础-学习笔记(一些命令记录)
VLAN介绍与实验
What is the website ICP record?
The hook of the operation of the selenium module
【考研结束第一天,过于空虚,想对自己进行总结一下】