LeetCode中常用语言的一些基本方法记录
文章目錄
- LeetCode中常用語言的一些基本方法記錄
- Java
- 數組
- 數組的常用操作及方法
- Arrays工具類
- Collections類常用方法總結
- 二維數組
- 字符串常用屬性及方法
- JavaScript
- 數組常用屬性及方法
- 字符串常用屬性及方法
- 創建二維數組
- Python
- 數組中常用屬性及方法
- 創建一個有規律的二維列表
- 字典
- 字符串常用方法
LeetCode中常用語言的一些基本方法記錄
注:語言一多有點容易混淆,所以梳理一下。
Java
數組
數組就是一種可以存儲大量數據類型相同的變量的數據結構,數組就是一個具有相同數據類型的數據集合。
數組中的數據必須是同一種數據類型的。
數組的基本要素:
數組名稱、數組元素、元素下標、數據類型
數組本身就是一個變量,數組名稱就是變量名,數組中保存的每一個數據都會有一個下標(從 0 開始)
數組的常用操作及方法
- 求數組的最大值
- 求數組的最小值
- 在數組的指定位置插入數據
- 對數組進行排序 : https://blog.csdn.net/hhhmonkey/article/details/107983663
獲取數組最大值
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值 // [I@7150bd4d 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個1System.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,其余默認為0System.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,7for(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/119648150equals方法:
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]e7.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]v8.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)); } 運行結果:59.lastIndexOfSubList(List source,List target)方法的使用與上例方法的使用相同
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)); } 運行結果:710.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]113.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
二維數組
二維數組簡單理解即一維數組中保存的值是另外一個一維數組。
變量、數據類型、流程控制、循環、數組。
字符串常用屬性及方法
參考:https://www.jianshu.com/p/7242679062d9
https://blog.csdn.net/weixin_30446613/article/details/96616981
- 字符串常用的屬性
- 字符串常用的方法
JavaScript
數組常用屬性及方法
//使用 JavaScript 關鍵詞 newvar 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:greenconsole.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()返回的是被彈出的值var arr4 = ["red", "blue", "green"];arr4.pop(); //"green" arr4 ======["red", "blue"] //shift() 方法會刪除首個數組元素,并把所有其他元素“位移”到更低的索引 返回的是被彈出的值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]; 返回的是選定的元素,該方法不會修改原數組 //合并數組//concat() 方法用于連接兩個或多個數組 該方法不會改變原有的數組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]按照第一個數字排序的 //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 //每一項都滿足條件時返回true,只要有一個不滿 足返回false var a16 = arr15.every(function(item,index){//return item >5; //必須要求每一個數字都>5才會返回true 否則返回false return item>1; //true}); //some() 只要有一個滿 足返回true,都不滿 足返回falsevar a17 = arr15.some(function(item,index){//return item >5; //只要有一個滿 足了條件,返回true return item==0; //false 都不滿足 返回false}); //reduce()乞丐要飯(類似累加)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)); //edocument.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 匹配成功后返回索引值document.write(str.indexOf('a')); //-1 沒有匹配成功則返回-1document.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() 方法可在字符串中抽取從開始下標開始的指定數目的字符document.write(str.substr(4)); //o wrolddocument.write(str.substr(2,4));//substr(start,length) "llo " //substring() 方法用于提取字符串中介于兩個指定下標之間的字符 document.write(str.substring(4)); //o wrolddocument.write(str.substring(2,4)); //substr(from,to) ll 不包括to //slice(start, end) 方法可提取字符串的某個部分,并以新的字符串返回被提取的部分document.write(str.slice(2,4)); //lldocument.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)); //defghconsole.log(str.substring(3,-4)); //abc*/ //toLowerCase() 方法用于把字符串轉換為小寫 //toUpperCase() 方法用于把字符串轉換為大寫 //trim() 方法用于刪除字符串的頭尾空格var str5 = ' hello ';document.write(str5.trim()); //hellohttps://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
數組中常用屬性及方法
參考:https://blog.csdn.net/qq_36134318/article/details/80729949
創建一個有規律的二維列表
[[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'] = '泰國' #同一個鍵不能出現兩次,否則后者覆蓋前者 #刪除: del dict01['sex']
字符串常用方法
參考:https://blog.csdn.net/weixin_43158056/article/details/92798114
總結
以上是生活随笔為你收集整理的LeetCode中常用语言的一些基本方法记录的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: LeetCode练习及自己理解记录(1)
- 下一篇: 记录工作中第一次解决bug的小事