美团2021年3月13日笔试(技术综合-后台方向)1-2
1、翻转矩阵
b[j,i]=a[i,j]
输入:1、n+1行
2、第一行 n,m表示n行m列矩阵
2、第一行 n,m表示n行m列矩阵
3、下面有n行,每行有m个整数
输出:m*n矩阵
输出:m*n矩阵
输入:3 2
1 2
3 4
5 6
输出:1 3 5
2 4 6
import java.util.Scanner; public class Main{ public static void main(String[] args) { Scanner sc = new Scanner(System.in); int[] first = new int[2]; while (sc.hasNext()){ for (int i = 0; i <2 ; i++) { first[i] = sc.nextInt(); } break; } int n = first[0],m = first[1]; int[][] omat = new int[n][m]; for (int i = 0; i <n ; i++) { while (sc.hasNext()){ for (int j = 0; j < m; j++) { omat[i][j] = sc.nextInt(); } break; } } int[][] res =solver(n,m,omat); for (int i = 0; i < m; i++) { for (int j = 0; j < n; j++) { System.out.print(((res[i][j])+" ")); } System.out.println(); } } public static int[][] solver(int n,int m,int[][] mat){ int[][] res = new int[m][n]; for (int i = 0; i < n ; i++) { for (int j = 0; j < m; j++) { res[j][i] = mat[i][j]; } } return res; } }
2、提取数字
从给定的字符串中提取数字并排序
输入:字符串
输出:k行数字
输入:as154kd12
输出:154
12
1、判断是否是数字
0<=s.charAt(i)-'0'<=9
2、使用双指针,第一个指向数字的起始索引,第二个指向数字的结束索引
字符串长度为n
f=b=0
while(f<n&&b<n){
如果 s.charAt(f)是数字
从f开始遍历字符串,停在非字符串处或字符串末尾得到数字结束索引并更新初始索引
如果不是数字
f+=1
}
3、计算数字大小
ans=0;
from f to b
ans = ans*10+s.charAt(i)-'0'
4、排序
Array.sort(arr)
package 三月十三.提取数字; import java.util.Arrays; import java.util.Deque; import java.util.LinkedList; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String s = sc.next(); solve(s); } public static void solve(String s){ int n = s.length(); int first=0; int second=0; Deque<int[]> deq = new LinkedList<>(); while(second<n&&first<n){ int[] index = new int[2]; if(s.charAt(first)-'0'<=9){ index[0]=first; for (second = index[0]; second < n ; second++) { if (first==n-1){ index[1] = first; break; } if (s.charAt(second)-'0'>9){ index[1] = second-1; first = second; break; } if (s.charAt(second)-'0'<=9&& second==n-1){ index[1] = second; } } deq.push(index); } first +=1; } int[] res =new int[deq.size()]; int in =0; while(!deq.isEmpty()){ int[] ind = deq.pop(); int i_first = ind[0]; int b_second = ind[1]; int ans = 0; for (int i = i_first; i <= b_second; i++) { //ans = s.charAt(i)-'0'; ans = 10*ans + (s.charAt(i)-'0'); } res[in] = ans; in+=1; } Arrays.sort(res); for (int j = 0; j < res.length; j++) { System.out.println(res[j]); } } }