目录
刷题
第一件事 : 牛客网需要验证输入数据内容和类型,leetcode直接开始下一步。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
while(in.hasNext()){
System.out.println(in.next());
System.out.println(in.next().getClass());
}
}
}
第二件事再根据输入与输出格式,进行对数据结构的选择和解决答案 : 先解决问题再优化为主要思想。例如,题目中提到有同学名字和成绩,根据同学成绩排序,则需要选择有序的数据结构,且有键和值可以放同学名字和成绩, 先选择TreeMap去解决问题,之后若有多余的时间再进行时间和空间的优化。
去重
1. 使用HashSet
特点 : 无序,无重复元素
HashSet 基于 HashMap 来实现的,是一个不允许有重复元素的集合。
HashSet 允许有 null 值。
HashSet 是无序的,即不会记录插入的顺序。
HashSet 不是线程安全的, 如果多个线程尝试同时修改 HashSet,则最终结果是不确定的。 您必须在多线程访问时显式同步对 HashSet 的并发访问。
HashSet 实现了 Set 接口。
// 引入 HashSet 类
import java.util.HashSet;
// 创建HashSet对象
HashSet<String> sites = new HashSet<String>();
// 添加元素 : add()
sites.add("https://www.google.com");
// 判断元素是否存在于集合当中 : contains()
System.out.println(sites.contains("google"));
// 删除集合中的元素 : remove() 删除成功返回 true,否则为 false
sites.remove("Taobao");
// 删除所有元素 : clear()
sites.clear();
// 遍历可使用for-each
for (String i : sites) {
System.out.println(i);
}
排序
1. 数组
2. TreeMap, 用法与HashMap差不多,区别在于TreeMap可以自动根据键升序排序。
去重+排序(使用库)
1. Set
// 去重
Set<Character> set = new HashSet<>();
set.add('2');
set.add('1');
set.add('5');
set.add('3');
set.add('4');
set.add('4');
System.out.println(set.toString());
// Lambda降序排序
Set<Character> sortSet = new TreeSet<Character>((o1, o2) -> o2.compareTo(o1));
// Lambda正序排序
// Set<Character> sortSet = new TreeSet<Character>((o1, o2) -> o1.compareTo(o2));
sortSet.addAll(set);
System.out.println(sortSet.toString());
算法优势
1. HashMap : 无序、可以实现高效访问的数据结构、CRUD方便。
// 基础方法
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
// 创建并初始化
HashMap<String, String> capitalCities = new HashMap<String, String>();
// 添加键和值 (Country, City)
capitalCities.put("England", "London");
capitalCities.put("Germany", "Berlin");
capitalCities.put("Norway", "Oslo");
capitalCities.put("USA", "Washington DC");
System.out.println(capitalCities);
// 获取元素
capitalCities.get("England");
// 删除元素
capitalCities.remove("England");
// 删除所有元素
capitalCities.clear();
// 遍历HashMap : 打印键
for (String i : capitalCities.keySet()) {
System.out.println(i);
}
// 遍历HashMap : 打印值
for (String i : capitalCities.values()) {
System.out.println(i);
}
// 遍历HashMap : 打印键和值
for (String i : capitalCities.keySet()) {
System.out.println("key: " + i + " value: " + capitalCities.get(i));
}
// 获取Map中最小的值
Collections.min(myMap.values());
}
}
// 给HashMap根据键 递增值,以下为现阶段的所有方法
// Source : https://stackoverflow.com/questions/81346/most-efficient-way-to-increment-a-map-value-in-java
// ContainsKey
import java.util.HashMap;
import java.util.Map;
...
Map<String, Integer> freq = new HashMap<String, Integer>();
...
int count = freq.containsKey(word) ? freq.get(word) : 0;
freq.put(word, count + 1);
// TestForNull
import java.util.HashMap;
import java.util.Map;
...
Map<String, Integer> freq = new HashMap<String, Integer>();
...
Integer count = freq.get(word);
if (count == null) {
freq.put(word, 1);
}
else {
freq.put(word, count + 1);
}
// AtomicLong
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.atomic.AtomicLong;
...
final ConcurrentMap<String, AtomicLong> map =
new ConcurrentHashMap<String, AtomicLong>();
...
map.putIfAbsent(word, new AtomicLong(0));
map.get(word).incrementAndGet();
// Trove
import gnu.trove.TObjectIntHashMap;
...
TObjectIntHashMap<String> freq = new TObjectIntHashMap<String>();
...
freq.adjustOrPutValue(word, 1, 1);
// MutableInt
import java.util.HashMap;
import java.util.Map;
...
class MutableInt {
int value = 1; // note that we start at 1 since we're counting
public void increment () { ++value; }
public int get () { return value; }
}
...
Map<String, MutableInt> freq = new HashMap<String, MutableInt>();
...
MutableInt count = freq.get(word);
if (count == null) {
freq.put(word, new MutableInt());
}
else {
count.increment();
}
字符串操作
1. 读取内容/处理数据 : BufferedReader(接收任意长度的数据,而且避免乱码产生)
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
2. 统一字符大小写(toLowerCase())
3. 删除字符串之间的空格(或其他字符)
String str = " hi world ~ ";
// 去除头尾空格
// 方法1:str.trim()
System.out.println("1--->"+str.trim());
// 去除所有空格
// 方法2:str.repalce(" ","")
System.out.println("2--->"+str.replace(" ", ""));
// 方法3:str.repalceAll(" ","")
System.out.println("3--->"+str.replaceAll(" ", ""));
// 方法4:str.repalceAll(" +","")
System.out.println("4--->"+str.replaceAll(" +", ""));
// 方法5:str.repalceAll("\\s*","")
System.out.println("4--->"+str.replaceAll("\\s*", ""));
4. 验证是否为空或null
// 是否为空
if (str.length() == 0);
if (str.equals(""));
// 是否为null
if (str == null);
// 既非空也非null : 先判断是否为空 否则length调用导致错误
if (str != null && str.length() != 0);
// 使用StringUtils库
if (StringUtils.isNotBlank(str));
5. 处理换行符(例如 : 切割每个段落或每行为数组内的元素,看需求怎么定义)
6. 处理特殊字符(*%之类的一般直接删除,除非是名称密码等特殊情况则是另一种处理办法)
- String通过特定字符分割为数组 : splite(),注意 : "."要变成"\\.",因为.是正则表达式。
public static String[] splitCoordinate(String input){
// 单一使用
String[] words = input.split(";");
// 配合读取用户输入使用
// String[] in = bf.readLine().split(";");
return words;
}
7. 正则表达式匹配(对新手来说,若语料量大可使用,否则没必要,验证正则式的时间还不如直接使用最基础的办法快) 教程 : Java 正则表达式 | 菜鸟教程
8. String包含String : equals()
String s1 = "12345";
String s2 = "1234";
if(s1.equals(s2)){
System.out.println("true");
}else{
System.out.println("false");
}
9. String包含char : contains()
String myStr = "Hello";
System.out.println(myStr.contains("Hel")); // true
System.out.println(myStr.contains("e")); // true
System.out.println(myStr.contains("Hi")); // false
10. String通过位置分割为子字符String : substring()
// 分割字母字符和数字坐标
String coor = "A10";
String letter = coor.substring(0, 1); // [) : A
String digit = coor.substring(1, coor.length()); // 10
11. 判断String是否为数字 : ascii码
public static boolean isNumeric(String str){
for(int i = str.length(); --i >= 0;){
int chr = str.charAt(i);
if(chr < 48 || chr > 57)
return false;
}
return true;
}
12. 删除/替代String内的某个char : replace()
str.replace(char oldCharacter, char newCharacter)
13. 判断字符串中是否包含指定的字符或字符串 : contains()
进制操作
描述 | 代码 | 转换后的类型 |
十进制转成十六进制 | Integer.toHexString(int i) | String |
十进制转成八进制 | Integer.toOctalString(int i) | String |
十进制转成二进制 | Integer.toBinaryString(int i) | String |
十六进制转成十进制 | Integer.valueOf("FFFF",16) | Integer |
八进制转成十进制 | Integer.valueOf("876",8) | Integer |
二进制转十进制 | Integer.valueOf("0101",2) | Integer |
二进制转八进制 | (new BigInteger("1010",2)).toString(8) | String |
二进制转十进制 | (new BigInteger("1010",2)).toString(10) | String |
二进制转十六进制 | (new BigInteger("11000001",2)).toString(16) | String |
详细的知识点了解进制 : 链接
数据操作
1. 类型判断 : getClass()
自测题目
1. 字符串 : 牛客网 : 坐标移动(题解 | #HJ17 坐标移动#_牛客博客)
2. 字符串 : 牛客网 : 整数与IP地址间的转换(题解 | #整数与IP地址间的转换#_牛客博客)
参考文章 :
2. 【JAVA8】Set排序四种写法-倒序_the_fool_的博客-CSDN博客_set倒序
5. java判断字符串不为空和null的方法_Bworry的博客-CSDN博客_判断字符串非空非null
6. Java IO之:BufferedReader(超详细解析,使用方法说明)_Hi丶ImViper的博客-CSDN博客_bufferedreader