当前位置:网站首页>Puge -- understanding of getordefault() method
Puge -- understanding of getordefault() method
2022-06-28 06:32:00 【Dear-JC】
analysis getOrDefault() Method
Introduce
getOrDefault(Object key, V defaultValue)
In operation Map When the collection , Determine whether this is stored key. If there is a corresponding value value , If it does not exist , Then the user-defined default value is returned defaultValue value .
Code implementation
LeetCode The first 387 topic : The first unique character in the string
public static void main(String[] args) {
String s = "loveleetcode";
System.out.println(firstUniqChar(s));
}
public static int firstUniqChar(String s) {
Map<Character, Integer> frequency = new HashMap<Character, Integer>();
for (int i = 0; i < s.length(); ++i) {
char ch = s.charAt(i);
// When this key when , Just use this value value , There is no default defaultValue
frequency.put(ch, frequency.getOrDefault(ch, 0) + 1); // Put the previous values in , By default +1 Calculation , Press... Only when there is a repetition ch Of value Calculation .
}
for (int i = 0; i < s.length(); ++i) {
if (frequency.get(s.charAt(i)) == 1) {
// Returns the specified character , Treat characters as key Go back
return i;
}
}
return -1; // Returns the last value
}
The end
边栏推荐
- Error reporting - resolve core JS / modules / es error. cause. JS error
- Teach you how to use UCOS
- 选拔赛题目代码
- Yolact++ pytoch environment
- RN7302三相电量检测(基于STM32单片机)
- Apple MDM bypass jailfree bypass MDM configuration lock free
- 助力涨点 | YOLOv5结合Alpha-IoU
- Linux MySQL implements root user login without password
- Install and manage multiple versions of PHP under mac
- ROS rviz_satellite功能包可视化GNSS轨迹,卫星地图的使用
猜你喜欢

Yolov5 adds a small target detection layer

YOLOv5增加小目标检测层

Yygh-7-user management

【Paper Reading-3D Detection】Fully Convolutional One-Stage 3D Object Detection on LiDAR Range Images

AutoCAD C # Polyline Small Sharp angle Detection

助力涨点 | YOLOv5结合Alpha-IoU

Causes of wechat applet compilation page blank bug

Online facing such an online world, the only limitation is our imagination

death_ satan/hyperf-validate

MySQL(一)——安装
随机推荐
【星海出品】 运维巡检合集
4~20mA输入/0~5V输出的I/V转换电路
The code is correct, and the rendering page does not display the reason
Use the SQL SELECT count distinct query statement to count the total number of unique values of a field in the database
CAD secondary development +nettopologysuite+pgis reference multi version DLL
API learning of OpenGL (2006) glclientactivetexture
Working principle of es9023 audio decoding chip
AttributeError: 'callable_iterator' object has no attribute 'next'
三极管驱动无刷电机
socke.io長連接實現推送、版本控制、實時活躍用戶量統計
KMP string
Some habits of it veterans in the workplace
图片按日期批量导入WPS表格
普歌 -- getOrDefault()方法理解
socke.io长连接实现推送、版本控制、实时活跃用户量统计
AttributeError: 'callable_ iterator' object has no attribute 'next'
Idea automatically adds comments when creating classes
5-minute NLP: summary of time chronology from bag of words to transformer
Yygh-7-user management
Uni app wechat applet sharing function