笔试:输入输出
█ 0.代码函数:
○ 0.1 边界
Integer.MAX_VALUE
int[] preorder ==> preorder == null || preorder.length == 0
char[][] board ==> board == null
int[][] matrix ==> (matrix == null || matrix.length == 0) || (matrix.length == 1 && matrix[0].length == 0)
String s ==> s == null || s.length() == 0
○ 0.2 牛客网 数据结构转换
for (char c : t.toCharArray()) need.put(c, need.getOrDefault(c, 0) + 1);
1)char[] str 转 String:
StringBuilder word = new StringBuilder();
for(int i = 0; i < str.length; i++){
word.append(str[i]);
}
2)String 转 char[]
转int : str.charAt(j) - '0'
bndry = Integer.MAX_VALUE / 10;
// 在此轮拼接后是否超过 2147483647
if(res > bndry || res == bndry && str.charAt(j) > '7') //负数问题
return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
res = res * 10 + (str.charAt(j) - '0');
char[] str = s.trim().toCharArray();
char[][] chs = {"abcde".toCharArray(), "fghij".toCharArray(), "klmno".toCharArray(),
"pqrst".toCharArray(), "uvwxy".toCharArray(), {'z'}};
3)一维数组(char[] , int r, int c) 转 二维数组 char[][]
char[][] board = new char[rows][cols];
int index = 0;
int x = 0;
for(int i = 0 ; i < matrix.length; i++){
if(index >= cols){
index = 0;
x++;
}
board[x][index] == matrix[i];
index++;
}
4)StringBuilder 转 String
res.toString().trim();
○ 0.3 函数名称:
Character.isDigit()
int[] nums;
for(int num:nums){
return new int[] { nums[i], nums[j] };
List<int[]> res = new ArrayList<>();
List<List<Integer>> triangle;;; triangle.get(i).get(j); 数组是变长的。
1 java中的length属性是针对数组说的;
2 java中的length()方法是针对字符串String说的, 字符串的长度则用到length()这个方法.
3.java中的size()方法是针对泛型集合说的, 泛型有多少个元素.
String []list={"ma","cao","yuan"}; list.length
String a="macaoyuan"; a.length()
List<Object> array=new ArrayList(); array.size();
isEmpty() -> String ; Stack
String---> charAt(0) ; subString(2) 起点; subString(1,3); .length() ; compareTo()
string.toCharArray();
Integer.parseInt(string)
linkedList.toArray(new String[list.size()])
List<Integer> list = inorder(root, new ArrayList<>());
//List<int[]> res = new ArrayList<>();
//int[] arr = new int[j-i];
//res.add(arr);
//res.toArray(new int[res.size()][]);
string[i] = String.valueOf(int[i]); str[i].equals("")
String[] strs = s.trim().split(" "); // 删除首尾空格,分割字符串
StringBuilder append("") string=stringbuilder.toString().trim();
public String reverseWords(String s) {
String[] strs = s.trim().split(" "); // 删除首尾空格,分割字符串
StringBuilder res = new StringBuilder();
for(int i = strs.length - 1; i >= 0; i--) { // 倒序遍历单词列表
if(strs[i].equals("")) continue; // 遇到空单词则跳过
res.append(strs[i] + " "); // 将单词拼接至 StringBuilder
}
return res.toString().trim(); // 转化为字符串,删除尾部空格,并返回
}
public String reverseLeftWords(String s, int n) {
return s.substring(n, s.length()) + s.substring(0, n);
}
public String reverseLeftWords(String s, int n) {
StringBuilder res = new StringBuilder();
for(int i = n; i < n + s.length(); i++)
res.append(s.charAt(i % s.length()));
return res.toString();
}
Map<Character, Integer> dic = new HashMap<>(); dic.containsKey(s.charAt(j)) ; dic.get(s.charAt(j)) ; dic.put(s.charAt(j), j);
Map<Integer, Integer> map = new HashMap<>();
class Solution {
public int singleNumber(int[] nums) {
Map<Integer, Integer> map = new HashMap<>();
for(int i = 0; i < nums.length; i++){
int key = nums[i];
if(!map.containsKey(key)){
map.put(key, 1);
}else {
map.put(key, map.get(key)+1);
}
}
for(Map.Entry<Integer, Integer> entry : map.entrySet()){
if(entry.getValue() == 1){
return entry.getKey();
}
}
return -1;
}
}
[]preorder --> Arrays.copyOfRange(preorder, 1, index+1) ; Arrays.sort(nums); for(int num:nums)
ArrayList --> add() get(i) .add(0, node.val); //从头部插入 ; remove(index) ;
List<int[]> res = new ArrayList<>(); res.add(arr一维); res.toArray(new int[res.size()][]);
LinkedList<List<Integer>> res = new LinkedList<>();
LinkedList<Integer> path = new LinkedList<>(); res.add(new LinkedList(path));
Queue<TreeNode> queue = new LinkedList<>() {{ add(root); }};
Queue<TreeNode> queue = new LinkedList<>();
queue.remove(); add(xx);
// 最小堆 Queue<Integer> queue = new PriorityQueue<>( (o1,o2) -> (o2.compareTo(o1))); poll() add() size()
Deque<Integer> deque = new LinkedList<>();
deque.peekFirst(); .removeFirst(); .peekLast(); .removeLast();
queue.add(value); deque.removeLast(); deque.add();
queue.offer(value); deque.pollLast(); deque.offerLast();
stack push() pop() isEmpty()
HashMap get() put()
HashSet<Character> set = new HashSet<>(); set.contains() add()
# █ 1.普通示例:
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner in=new Scanner(System.in);
while(in.hasNext()){
int n=in.nextInt();
int m=in.nextInt();
int[][] arr=new int[m][3];
for (int i=0;i<m;i++){
for (int j=0;j<3;j++){
arr[i][j]=in.nextInt();
}
}
int res=number(n,arr);
System.out.println(res);
}
}
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Main a = new Main();
Scanner scanner = new Scanner(System.in);
ArrayList<Integer> ret = new ArrayList<Integer>();
int n,m;
while (scanner.hasNextInt()){
n=scanner.nextInt();
m=scanner.nextInt();
int[][] relationShip=new int[m][3];
for (int i = 0; i < m; i++) {
relationShip[i][0]=scanner.nextInt();
relationShip[i][1]=scanner.nextInt();
relationShip[i][2]=scanner.nextInt();
}
System.out.println(a.getResult(n,m,relationShip));
}
}
█ 2.Hashmap 字符串的问题
scanner.hasNext(); String s = scanner.next(); sc.nextInt(); scanner.hasNextInt()
import java.util.*;
public class Main
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
while (sc.hasNext())
{
ArrayList<Integer> price = new ArrayList<>();
int goodsNum = 0, buyNum = 0;
HashMap<String, Integer> buyList = new HashMap<>();
goodsNum = sc.nextInt();
buyNum = sc.nextInt();
for (int i = 0; i < goodsNum; i++)
{
price.add(sc.nextInt());
}
for (int i = 0; i < buyNum; i++)
{
String good = sc.next();
if (buyList.containsKey(good))
{
int count = buyList.get(good);
buyList.put(good, ++count);
}
else
buyList.put(good, 1);
}
int maxValue = 0;
int minValue = 0;
Collection collection = buyList.values();
ArrayList<Integer> list = new ArrayList<>(collection);
Collections.sort(list);
Collections.sort(price);
for (int i = 0; i < list.size(); i++)
{
minValue += list.get(list.size() - i - 1) * price.get(i);
maxValue += list.get(list.size() - i - 1) * price.get(price.size() - i - 1);
}
System.out.printf(minValue + " " + maxValue + "\n");
}
sc.close();
}
}
█ 3.路径规划 字符串拼接
scanner.hasNext(); String s = scanner.next();
String charAt
StringBuffer step append
List<String> steps = new ArrayList<>(); steps.add(step.toString());
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()) {
String s = scanner.next();
String e = scanner.next();
int diffLR = s.charAt(0) - e.charAt(0);
int diffUD = s.charAt(1) - e.charAt(1);
List<String> steps = new ArrayList<>();
while (diffLR != 0 || diffUD != 0) {
StringBuffer step = new StringBuffer("");
if (diffLR < 0) {
step.append("R");
diffLR++;
} else if (diffLR > 0) {
step.append("L");
diffLR--;
}
if (diffUD < 0) {
step.append("U");
diffUD++;
} else if (diffUD > 0) {
step.append("D");
diffUD--;
}
steps.add(step.toString());
}
System.out.println(steps.size());
for (String step : steps){
System.out.println(step);
}
}
scanner.close();
}
}
█ 4.TreeNode getOrDefault Map.Entry
import java.util.Map;
import java.util.Scanner;
import java.util.TreeMap;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int n = sc.nextInt();
int s = sc.nextInt();
TreeMap<Integer, Integer> buy = new TreeMap<>((a, b) -> b - a);
TreeMap<Integer, Integer> sell = new TreeMap<>((a, b) -> b - a);
for (int i = 0; i < n; ++i) {
if ("B".equals(sc.next())) {
int key = sc.nextInt();
int value = sc.nextInt();
buy.put(key, buy.getOrDefault(key, 0) + value);
}else {
int key = sc.nextInt();
int value = sc.nextInt();
sell.put(key, sell.getOrDefault(key, 0) + value);
}
}
int cnt = 0;
for(Map.Entry<Integer, Integer> entry: sell.entrySet()) {
System.out.println("S " + entry.getKey() + " " + entry.getValue());
++cnt;
if (cnt == s) {
break;
}
}
cnt = 0;
for (Map.Entry<Integer, Integer> entry: buy.entrySet()) {
System.out.println("B " + entry.getKey() + " " + entry.getValue());
++cnt;
if (cnt == s) {
break;
}
}
}
sc.close();
}
}
█ 5.公约数 进制数和
import java.util.Scanner;
public class Main {
public static int getGongYueShu(int a, int b) {// 求两个数的最大公约数
int t = 0;
if (a < b) {
t = a;
a = b;
b = t;
}
int c = a % b;
if (c == 0) {
return b;
} else {
return getGongYueShu(b, c);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
int num = sc.nextInt();
int sum = 0;
for (int i = 2; i < num; i++) {
int sum_ = getSum(num, i);// 将num转换成指定进制数然后各个位求和
sum += sum_;
}
int gongYueShu = getGongYueShu(sum, num - 2);
System.out.println(sum / gongYueShu + "/" + (num - 2) / gongYueShu);
}
sc.close();
}
private static int getSum(int num, int i) {
int sum = 0;
while (num != 0) {
int wei = num % i;
sum += wei;
num = num / i;
}
return sum;
}
}
█ 6.构建一个二叉树:
应该是要写类的
构建一个链表
靠技术实力称霸,千面鬼手大人万岁!