Java算法比賽常用方法實例總結(jié)
1. 開方:Math.sqrt(x);
2. x的a方:Math.pow(x,a);
3. 絕對值:Math.abs(x);
4. BigInteger:大數(shù)(加,減,乘,除,取余)
c.add(d) ; c.subtract(d);c.multiply(d);c.divide(d);c.mod(d)
5. 判斷回文:
public static void main(String[] args) { String list="12321"; StringBuilder str=new StringBuilder(list); if (str.reverse().toString().equals(list)){ System.out.println(true); } }
6. HashMap
(key,value)put,get,remove,獲取key使用keySet()
7.HashSet:去重
8.字符串相關(guān)
6.字符串轉(zhuǎn)化成字符數(shù)組
7.字符數(shù)組轉(zhuǎn)發(fā)成字符串 String helloString = new String(helloArray);
8.忽略字符串大小寫的比較方法,這就是 equalsIgnoreCase( )方法。同樣返回boolean值。
9.去除首尾空白字符串 trim()
10.包含子字符串contains
補充:hashMap按值排序
輸入
第一行 輸入n個字符串
其余n行 :n個字符串
輸出每個字符串從大到小出現(xiàn)次數(shù)
格式 出現(xiàn)次數(shù) - 字符串
eg:
5
2 -1 -1 22
1 11 66 0
1 28 74 35
3 35 28 7
2 -1 -1 22
實現(xiàn)代碼
import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Map.Entry; import java.util.Scanner; public class Main { public static void main(String[] args) { Map<String, Integer>map=new HashMap<>(); Scanner sca=new Scanner(System.in); int n=sca.nextInt(); sca.nextLine(); for(int i=0;i<n;i++) { String str=sca.nextLine(); int num=map.getOrDefault(str, 0)+1; map.put(str, num); } List<Map.Entry<String, Integer>>list=new ArrayList<>(); for(Map.Entry<String, Integer>mv:map.entrySet()) { list.add(mv); } Collections.sort(list,new Comparator<Map.Entry<String, Integer>>() { @Override public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) { // TODO Auto-generated method stub return o2.getValue()-o1.getValue(); } }); System.out.println(); for(int i=0;i<list.size();i++) { Map.Entry<String, Integer> mvEntry=list.get(i); String key=mvEntry.getKey(); Integer value=mvEntry.getValue(); System.out.println(value +" - "+ key); } } }
輸出
2 - 2 -1 -1 22
1 - 1 28 74 35
1 - 1 11 66 0
1 - 3 35 28 7
總結(jié)
到此這篇關(guān)于Java算法比賽常用方法的文章就介紹到這了,更多相關(guān)Java算法比賽方法內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java?lambda表達(dá)式與泛型整理總結(jié)
Lambda?表達(dá)式(lambda?expression)是一個匿名函數(shù),Lambda表達(dá)式基于數(shù)學(xué)中的λ演算得名。泛型編程,故如其名,是一個泛化的編程方式。其實現(xiàn)原理為程序員編寫一個函數(shù)/類的代碼示例,讓編譯器去填補出不同的函數(shù)實現(xiàn)2022-07-07基于springboot的RestTemplate、okhttp和HttpClient對比分析
這篇文章主要介紹了基于springboot的RestTemplate、okhttp和HttpClient對比分析,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-09-09關(guān)于break和continue以及l(fā)abel的區(qū)別和作用(詳解)
下面小編就為大家?guī)硪黄P(guān)于break和continue以及l(fā)abel的區(qū)別和作用(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-05-05在SpringBoot中如何利用Redis實現(xiàn)互斥鎖
當(dāng)我們利用Redis存儲熱點數(shù)據(jù)時,突然就過期失效或者被刪除了,導(dǎo)致大量請求同時訪問數(shù)據(jù)庫,增加了數(shù)據(jù)庫的負(fù)載,為減輕數(shù)據(jù)庫的負(fù)載我們利用互斥鎖,本文重點介紹在SpringBoot中如何利用Redis實現(xiàn)互斥鎖,感興趣的朋友一起看看吧2023-09-09Java Grpc實例創(chuàng)建負(fù)載均衡詳解
這篇文章主要介紹了Java Grpc實例創(chuàng)建負(fù)載均衡詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-03-03關(guān)于SpringBoot整合redis使用Lettuce客戶端超時問題
使用到Lettuce連接redis,一段時間后不操作,再去操作redis,會報連接超時錯誤,在其重連后又可使用,糾結(jié)是什么原因?qū)е碌哪?,下面小編給大家?guī)砹薙pringBoot整合redis使用Lettuce客戶端超時問題及解決方案,一起看看吧2021-08-08