0%

Java和Python语法

做算法题,经常用到的语法

常用语法

字符串排序

1
2
3
char[] charArray = str.toCharArray();
Arrays.sort(charArray);
String key = new String(charArray);
1
''.join(sorted(element))  # 字符串sorted后 ['a', 'd', 'f', 's']

字典操作

1
2
3
4
5
6
7
if (hashMap.containsKey(str_s)){
hashMap.get(str_s).add(str);
} else {
ArrayList<String> strings = new ArrayList<>();
strings.add(str);
hashMap.put(str_s, strings);
}
1
2
3
4
if t in dic:
dic[t].append(element)
else:
dic[t] = [element]