赞
踩
- import java.util.*;
-
- public class Main{
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- String s = in.nextLine();
- if(s.contains("0x")){
- s = s.substring(2);
- }
- Integer n = Integer.parseInt(s,16);
- System.out.println(n.toString());
- }
- }
- /*
- 16转10
- Integer.parseInt(string,16);
- 10转2
- Integer.toBinaryString(num);
- 10转8
- Integer.toOctalString(num);
- 10转16
- Integer.toHexString(num);
- */
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- String s = in.next();
- System.out.println(Integer.parseInt(s.substring(2),16));
- }
- import java.util.*;
-
-
- public class Solution {
- /**
- *
- * @param numbers int整型一维数组
- * @param target int整型
- * @return int整型一维数组
- */
- public int[] twoSum (int[] numbers, int target) {
- // write code here
- Map<Integer,Integer> map = new HashMap();
- int[] num = new int[2];
- for(int i = 0; i < numbers.length; i++){
- Integer sum = target - numbers[i];
- if(map.containsKey(sum)){
- num[0] = map.get(sum);
- num[1] = i+1;
- break;
- } else {
- map.put(numbers[i],i+1);
- }
- }
- return num;
- }
- }
- import java.util.*;
- public class Main{
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- int[] m= new int[n];
- for(int i = 0; i < n; i++){
- m[i] = in.nextInt();
- }
- in.close();
- Arrays.sort(m);
- System.out.println(m[0]);
- for(int i = 1; i < n; i++){
- if(m[i] != m[i-1]){
- System.out.println(m[i]);
- }
- }
- }
- }
- import java.util.*;
-
- public class Main{
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- String s = in.next();
- Set<String> set = new HashSet();
- for(int i = 0; i < s.length(); i++){
- String str = s.substring(i,i+1);
- set.add(str);
- }
- System.out.println(set.size());
- }
- }
- public class Solution {
- public int jumpFloor(int target) {
- if(target == 1 || target == 0){
- return 1;
- }
- if(target == 2){
- return 2;
- }
- return jumpFloor(target - 1) + jumpFloor(target - 2);
- }
- }
- import java.util.*;
- public class Solution {
- Map<Integer,Integer> map = new HashMap();
- public int jumpFloor(int target) {
- if(target == 1 || target == 0){
- return 1;
- }
- if(target == 2){
- return 2;
- }
- if(map.containsKey(target)){
- return map.get(target);
- } else{
- map.put(target,jumpFloor(target - 1) + jumpFloor(target - 2));
- return map.get(target);
- }
- }
- }
- import java.util.*;
-
- public class Main{
- public static void main(String[] args) {
- Scanner in = new Scanner(System.in);
- String s = in.nextLine();
- in.close();
- String[] str = s.split(";");
- int[] n = new int[2];
- java.util.regex.Pattern pattern = java.util.regex.Pattern.compile("[0-9]*");
- for(String s1 : str){
- if(s1.length() <= 3 && s1.length() >= 2 && pattern.matcher(s1.substring(1)).matches()){
- String s2 = s1.substring(0,1);
- Integer s4 = Integer.parseInt(s1.substring(1));
- if("A".equals(s2)){
- n[0] = n[0] - s4;
- }else if("D".equals(s2)){
- n[0] = n[0] + s4;
- }else if("W".equals(s2)){
- n[1] = n[1] + s4;
- }else if("S".equals(s2)){
- n[1] = n[1] - s4;
- }
- }
- }
- System.out.println(n[0]+","+n[1]);
- }
- }
正则表达式:
- import java.util.*;
- import java.util.regex.*;
-
- public class Main{
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- while(in.hasNext()){
- String s = in.next();
- if(s.length() <= 8){
- System.out.println("NG");
- continue;
- }
- if(getMatch(s)){
- System.out.println("NG");
- continue;
- }
- if(getString(s,0,3)){
- System.out.println("NG");
- continue;
- }
- System.out.println("OK");
- }
- }
-
- private static boolean getString(String str,int l,int r){
- if(r >= str.length()){
- return false;
- }
- if(str.substring(r).contains(str.substring(l,r))){
- return true;
- } else {
- return getString(str,l+1,r+1);
- }
- }
-
- private static boolean getMatch(String str){
- int count = 0;
- Pattern p1 = Pattern.compile("[A-Z]");
- if(p1.matcher(str).find()){
- count++;
- }
- Pattern p2 = Pattern.compile("[a-z]");
- if(p2.matcher(str).find()){
- count++;
- }
- Pattern p3 = Pattern.compile("[0-9]");
- if(p3.matcher(str).find()){
- count++;
- }
- Pattern p4 = Pattern.compile("[^a-zA-Z0-9]");
- if(p4.matcher(str).find()){
- count++;
- }
- if(count >= 3){
- return false;
- }
- return true;
- }
- }
- import java.util.*;
-
- public class Main{
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- String s = in.next();
- in.close();
- Map<Character,Integer> map = new HashMap();
- StringBuffer str = new StringBuffer();
- for(char c : s.toCharArray()){
- map.put(c,map.getOrDefault(c,1)+1);
- }
- int min = Integer.MAX_VALUE;
- for(int e : map.values()){
- min = Math.min(min,e);
- }
- for(char c : s.toCharArray()){
- if(map.get(c) != min){
- str.append(c);
- }
- }
- System.out.println(str);
- }
- }
- import java.util.*;
-
- public class Main{
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- while(in.hasNext()){
- String s = in.next();
- if(s.contains(".")){
- System.out.println(toNumber(s));
- }else {
- System.out.println(toIp(s));
- }
- }
- }
-
- private static String toIp(String str){
- Long num = Long.parseLong(str);
- String res = Long.toBinaryString(num);
- StringBuffer sf = new StringBuffer();
- if(res.length() != 32){
- for(int i = 0; i < 32 - res.length(); i++ ){
- sf.append("0");
- }
- }
- sf.append(res);
- String s = sf.toString();
- StringBuffer sf1 = new StringBuffer();
- return toIp2(s,0,8,sf1);
- }
-
- private static String toIp2(String str,int l,int r, StringBuffer sf1){
- String s = str.substring(l,r);
- Long num = Long.parseLong(s,2);
- if(r == str.length()){
- sf1.append(num);
- }else {
- sf1.append(num + ".");
- toIp2(str,r,r+8,sf1);
- }
- return sf1.toString();
- }
-
- private static Long toNumber(String str){
- String[] s = str.split("\\.");
- StringBuffer sf = new StringBuffer();
- for(String s1 : s){
- Long num = Long.parseLong(s1);
- String res = Long.toBinaryString(num);
- if(res.length() < 8){
- for(int i = 0; i < 8 - res.length(); i++){
- sf.append("0");
- }
- }
- sf.append(res);
- }
- return Long.parseLong(sf.toString(),2);
- }
- }
- 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[] nums = new int[n];
- for(int i = 0; i < n; i++){
- nums[i] = in.nextInt();
- }
- int flag = in.nextInt();
- Arrays.sort(nums);
- if(flag == 0){
- for(int i = 0; i < n; i++){
- System.out.print(nums[i] + " ");
- }
- System.out.println();
- } else {
- for(int i = n - 1; i >= 0; i--){
- System.out.print(nums[i] + " ");
- }
- System.out.println();
- }
- }
- }
- }
- import java.util.*;
-
- public class Main{
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- String s = in.nextLine();
- String[] str = s.split(" ");
- for(int i = 0; i < str.length; i++){
- if(str[i].length() > 1){
- StringBuffer sf = new StringBuffer();
- sf.append(str[i]);
- str[i] = sf.reverse().toString();
- }
- }
- for(int i = str.length -1; i >= 0 ; i--){
- System.out.print(str[i] + " ");
- }
- }
- }
- 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();
- TreeMap<Integer,Integer> map = new TreeMap();
- for(int i = 0; i < n; i++){
- int a = in.nextInt();
- int b = in.nextInt();
- map.put(a,map.getOrDefault(a,0) + b);
- }
- map.forEach((key,value) -> {
- System.out.println(key+" "+value);
- });
- }
- }
- }
- import java.util.*;
-
- public class Main{
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- List<String> list = new ArrayList();
- while(in.hasNext()){
- int n = in.nextInt();
- for(int i = 0; i < n; i++){
- list.add(in.next());
- }
- Collections.sort(list);
- for(String s : list){
- System.out.println(s);
- }
- }
- }
- }
- import java.util.*;
-
- public class Main{
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- String[] s = in.nextLine().split(" ");
- int n = Integer.parseInt(s[0]);
- String x = s[s.length - 2];
- int k = Integer.parseInt(s[s.length - 1]);
- List<String> list = new ArrayList();
- for(String str : s){
- if(isBrother(str,x)){
- list.add(str);
- }
- }
- int size = list.size();
- System.out.println(size);
- if(size >= k){
- Collections.sort(list);
- System.out.println(list.get(k - 1));
- }
- }
-
- private static boolean isBrother(String str,String x){
- if(str.equals(x) || str.length() != x.length()){
- return false;
- }
- char[] c = str.toCharArray();
- char[] ch = x.toCharArray();
- Arrays.sort(c);
- Arrays.sort(ch);
- return String.valueOf(c).equals(String.valueOf(ch));
- }
- }
- import java.util.*;
- /**
- * Definition for an interval.
- * public class Interval {
- * int start;
- * int end;
- * Interval() { start = 0; end = 0; }
- * Interval(int s, int e) { start = s; end = e; }
- * }
- */
- public class Solution {
- public ArrayList<Interval> merge(ArrayList<Interval> intervals) {
- ArrayList<Interval> res = new ArrayList();
- int size = intervals.size();
- if(size == 0){
- return res;
- }
- Collections.sort(intervals,new Comparator<Interval>(){
- public int compare(Interval o1, Interval o2){
- if(o1.start != o2.start){
- // 头升序
- return o1.start - o2.start;
- } else {
- // 尾升序
- return o1.end - o2.end;
- }
- }
- });
- res.add(intervals.get(0));
- int count = 0;
- for(int i = 1; i < size; i++){
- Interval o1 = intervals.get(i);
- Interval o2 = res.get(count);
- if(o1.start <= o2.end){
- if(o2.end < o1.end){
- o2.end = o1.end;
- }
- } else{
- res.add(o1);
- count++;
- }
- }
- return res;
- }
- }
- 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 flag = in.nextInt();
- String[] names = new String[n];
- int[] grades = new int[n];
- for(int i = 0; i < n; i++){
- names[i] = in.next();
- grades[i] = Integer.parseInt(in.next());
- }
- if(flag == 0){
- for(int i = 0; i<n; i++){
- for(int j = 0; j < n-1-i; j++){
- if(grades[j] < grades[j+1]){
- int g = grades[j];
- grades[j] = grades[j+1];
- grades[j+1] = g;
- String name = names[j];
- names[j] = names[j+1];
- names[j+1] = name;
- }
- }
- }
- }else {
- for(int i = 0; i<n; i++){
- for(int j = 0; j < n-1-i; j++){
- if(grades[j] > grades[j+1]){
- int g = grades[j];
- grades[j] = grades[j+1];
- grades[j+1] = g;
- String name = names[j];
- names[j] = names[j+1];
- names[j+1] = name;
- }
- }
- }
- }
- for(int i = 0; i< n; i++){
- System.out.println(names[i] + " " + grades[i]);
- }
- }
- }
- }
- import java.util.*;
-
-
- public class Solution {
- /**
- *
- * @param s string字符串
- * @return bool布尔型
- */
- public boolean isValid (String s) {
- // write code here
- if(s.length() < 2){
- return false;
- }
- Stack<Character> st = new Stack();
- for(int i = 0; i < s.length(); i++){
- if(s.charAt(i) == '('){
- st.push(')');
- }
- else if(s.charAt(i) == '['){
- st.push(']');
- }
- else if(s.charAt(i) == '{'){
- st.push('}');
- }
- else if(st.isEmpty() || st.pop() != s.charAt(i)){
- return false;
- }
- }
- return st.isEmpty();
- }
- }
- class Solution {
- public int maxDepth(String s) {
- Stack<Character> st = new Stack();
- int max = 0;
- for(int i = 0; i < s.length(); i++){
- if(s.charAt(i) == '('){
- st.push(')');
- }
- else if(s.charAt(i) == ')'){
- max = Math.max(max,st.size());
- st.pop();
- }
- }
- return max;
- }
- }
- class Solution {
- public String[] permutation(String S) {
- List<String> list = new ArrayList();
- char[] c = S.toCharArray();
- Arrays.sort(c);
- boolean[] used = new boolean[c.length];
- dfs(list,new StringBuilder(),used,c);
- String[] str = new String[list.size()];
- for(int i = 0; i < str.length; i++){
- str[i] = list.get(i);
- }
- return str;
- }
-
- public void dfs(List<String> list,StringBuilder sb, boolean[] used, char[] c){
- if(sb.length() == c.length){
- list.add(sb.toString());
- return;
- }
- for(int i = 0; i < c.length; i++){
- // 第i个字符未被使用
- if(!used[i]){
- //i大于0 且 第 i-1个字符跟第i个字符相同 且 第i-1个字符未被使用
- if(i > 0 && c[i] == c[i-1] && !used[i - 1]){
- continue;
- } else {
- sb.append(c[i]);
- used[i] = true;
- dfs(list,sb,used,c);
- used[i] = false;
- sb.deleteCharAt(sb.length() - 1);
- }
- }
- }
- }
- }
- class Solution {
- public List<List<Integer>> combine(int n, int k) {
- int[] nums = new int[n];
- for(int i = 1; i <= n; i++){
- nums[i-1] = i;
- }
- List<List<Integer>> res = new ArrayList();
- dfs(k,res,new ArrayList(),nums,0);
- return res;
- }
-
- public void dfs(int k,List<List<Integer>> res,List<Integer> list, int[] n, int j){
- if(list.size() == k){
- List<Integer> l = new ArrayList();
- l.addAll(list);
- res.add(l);
- return;
- }
- for(int i = j; i < n.length; i++){
- list.add(n[i]);
- dfs(k,res,list,n,i+1);
- list.remove(list.size() - 1);
- }
- }
- }
- class Solution {
- public int findLengthOfLCIS(int[] nums) {
- int max = 1;
- int count = 1;
- for(int j = 1; j < nums.length; j++){
- if(nums[j] > nums[j - 1]){
- count++;
- }
- if(nums[j] <= nums[j - 1] || j == nums.length - 1){
- max = Math.max(max,count);
- count = 1;
- }
- }
- return max;
- }
- }
- import java.util.*;
-
-
- public class Solution {
- public int getLongestPalindrome (String A) {
- int n = A.length();
- if(n < 2){
- return n;
- }
- int max = 1;
- for(int i = 0; i < n; i++){
- max = Math.max(max,Math.max(fun(A,i,i),fun(A,i,i+1)));
- }
- return max;
- }
-
- public int fun(String s,int begin,int end){
- while(begin >= 0 && end < s.length() && s.charAt(begin) == s.charAt(end)){
- begin--;
- end++;
- }
- return end - begin - 1;
- }
- }
- import java.util.*;
-
-
- public class Solution {
- public String minWindow (String S, String T) {
- if(S.contains(T)){
- return T;
- }
- int cnt = S.length() + 1;
- int[] hash = new int[128];
- for(int i = 0; i < T.length(); i++){
- hash[T.charAt(i)] -= 1;
- }
- int slow = 0,fast = 0,left = -1,right = -1;
- for(; fast< S.length(); fast++){
- char c = S.charAt(fast);
- hash[c]++;
- while(check(hash)){
- if(cnt > fast - slow + 1){
- cnt = fast - slow + 1;
- left = slow;
- right = fast;
- }
- c = S.charAt(slow);
- hash[c]--;
- slow++;
- }
- }
- if(left == -1){
- return "";
- }
- return S.substring(left,right + 1);
- }
-
- public boolean check(int[] hash){
- for(int i = 0; i < hash.length; i++){
- if(hash[i] < 0){
- return false;
- }
- }
- return true;
- }
- }
- import java.io.BufferedReader;
- import java.io.InputStreamReader;
-
- public class Main {
-
- public static void main(String[] args) throws Exception {
- BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
- String line;
- while ((line = reader.readLine()) != null) {
- int n = Integer.parseInt(line);
- String[] s = reader.readLine().split(" ");
- String[] s2 = reader.readLine().split(" ");
- int[] weight = new int[n];
- int[] number = new int[n];
- for (int i = 0; i < n; i++) {
- weight[i] = Integer.parseInt(s[i]);
- number[i] = Integer.parseInt(s2[i]);
- }
- boolean[] exist = new boolean[200000];
- int[] arr = new int[20000];
- arr[0]++;
- for (int i = 0; i < n; i++) {
- for (int j = 0; j < number[i]; j++) {
- int index = arr[0];
- if (!exist[weight[i]]) {
- exist[weight[i]] = true;
- arr[++arr[0]] = weight[i];
- }
- for (int k = 1; k <= index; k++) {
- int sum = arr[k] + weight[i];
- if (!exist[sum]) {
- exist[sum] = true;
- arr[++arr[0]] = sum;
- }
- }
- }
- }
- System.out.println(arr[0]);
- }
- }
- }
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- class Solution {
- public int[] levelOrder(TreeNode root) {
- if(root == null) return new int[0];
- Queue<TreeNode> queue = new LinkedList<TreeNode>();
- List<Integer> list = new ArrayList();
- queue.add(root);
- while(!queue.isEmpty()){
- TreeNode node = queue.poll();
- list.add(node.val);
- if(node.left != null){
- queue.add(node.left);
- }
- if(node.right != null){
- queue.add(node.right);
- }
- }
- return list.stream().mapToInt(Integer::intValue).toArray();
- }
- }
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- class Solution {
- public List<List<Integer>> levelOrder(TreeNode root) {
- List<List<Integer>> res = new ArrayList();
- if(root == null) return res;
- Queue<TreeNode> queue = new LinkedList<TreeNode>();
- queue.add(root);
- while(!queue.isEmpty()){
- List<Integer> list = new ArrayList();
- int size = queue.size();
- for(;size > 0; size--){
- TreeNode node = queue.poll();
- if(node != null){
- list.add(node.val);
- if(node.left != null){
- queue.add(node.left);
- }
- if(node.right != null){
- queue.add(node.right);
- }
- }
- }
- res.add(list);
- }
- return res;
- }
- }
- /**
- * Definition for a binary tree node.
- * public class TreeNode {
- * int val;
- * TreeNode left;
- * TreeNode right;
- * TreeNode(int x) { val = x; }
- * }
- */
- class Solution {
- public List<List<Integer>> levelOrder(TreeNode root) {
- List<List<Integer>> res = new ArrayList();
- if(root == null) return res;
- Queue<TreeNode> queue = new LinkedList<TreeNode>();
- queue.add(root);
- while(!queue.isEmpty()){
- List<Integer> list = new ArrayList();
- int size = queue.size();
- for(;size > 0; size--){
- TreeNode node = queue.poll();
- if(node != null){
- list.add(node.val);
- if(node.left != null){
- queue.add(node.left);
- }
- if(node.right != null){
- queue.add(node.right);
- }
- }
- }
- res.add(list);
- }
- for(int i=1;i<res.size();i+=2){
- Collections.reverse(res.get(i));
- }
- return res;
- }
- }
- import java.util.*;
-
- public class Main {
-
- public static void main(String[] args) {
- Scanner in=new Scanner(System.in);
- int a=in.nextInt();
- int b=in.nextInt();
- System.out.println(a*b/gcd(a,b));
- }
-
- private static int gcd(int a, int b) {
- return b==0?a:gcd(b,a%b);
- }
- }
- import java.util.*;
- public class Main {
-
- //这里包含了判断素数的方法
- //小技巧!!!素数不是偶数,那么和是素数的话就是奇数+偶数
- //那么可以分成两堆,一堆偶数,一堆奇数
- //匈牙利算法,先到先得 能让就让
- //有机会上,没机会创造机会也要上
- public static void main(String[] args) {
- // TODO Auto-generated method stub
- Scanner scan = new Scanner(System.in);
- while(scan.hasNext()){
- int n = scan.nextInt();
- int[] tempArray = new int[n];
- for(int i = 0; i < n; i++){
- tempArray[i] = scan.nextInt();
- }
- ArrayList<Integer> evens = new ArrayList<Integer>();
- ArrayList<Integer> odds = new ArrayList<Integer>();
- for(int i = 0; i < n; i++) {
- if((tempArray[i] & 1) != 1) {
- evens.add(tempArray[i]);
- }else {
- odds.add(tempArray[i]);
- }
- }
- //下面开始才是重头戏
- //用于标记那个奇数匹配了偶数,直接记录奇数的值,而不是奇数在奇数数组中的下标
- int[] evensMatch =new int[evens.size()];
- int result = 0;
- //遍历奇数去匹配偶数
- for(int i = 0; i < odds.size(); i++) {
- //每一步重新创建,也就是相当于清空
- //used数组用于标记某个某数位置是否
- int[] used = new int[evens.size()];
- //这里采用了匈牙利算法,先到先得
- if(find(odds.get(i), evens, used, evensMatch)) {
- result++;
- }
- }
- System.out.println(result);
- }
- }
-
- public static boolean isPrime(int num) {
- for(int i = 2; i <= Math.sqrt(num); i++) {
- if(num % i == 0) {
- return false;
- }
- }
- return true;
- }
-
- public static boolean find(int x, ArrayList<Integer> evens,int[] used, int[] evensMatch) {
- //遍历偶数
- //去检查当前传入的奇数能否与偶数哪些数匹配
- for(int i = 0; i < evens.size(); i++) {
- //如果当前偶数与传入的奇数匹配,并且当前偶数位还没有匹配过奇数
- if(isPrime(x + evens.get(i)) && used[i] == 0) {
- //设置当前偶数位匹配为true,也就是 1
- used[i] = 1;
- //如果第i个偶数没有伴侣
- //或者第i个偶数原来有伴侣,并且该伴侣能够重新找到伴侣的话(这里有递归调用)
- //则奇数x可以设置为第i个偶数的伴侣
- //这里采用了匈牙利算法,能让则让
- if(evensMatch[i] == 0 || find(evensMatch[i], evens, used, evensMatch)) {
- evensMatch[i] = x;
- return true;
- }
- }
- }
- //遍历完偶数都没有可以与传入奇数做伴侣的,该奇数只能孤独终老了
- return false;
- }
-
- }
- import java.util.*;
-
- public class Main {
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- int i = n/2;
- for(;i < n; i += 2){
- if(i % 2 == 0 && i != 2){
- i++;
- }
- if(isPrime(n - i) && isPrime(i)){
- System.out.println(n - i);
- System.out.println(i);
- break;
- }
- }
- }
-
- public static boolean isPrime(int num) {
- for(int i = 2; i <= Math.sqrt(num); i++) {
- if(num % i == 0) {
- return false;
- }
- }
- return true;
- }
- }
- class Solution {
- public int orangesRotting(int[][] grid) {
- int M = grid.length;
- int N = grid[0].length;
- Queue<int[]> queue = new LinkedList<>();
-
- int count = 0; // count 表示新鲜橘子的数量
- for (int r = 0; r < M; r++) {
- for (int c = 0; c < N; c++) {
- if (grid[r][c] == 1) {
- count++;
- } else if (grid[r][c] == 2) {
- queue.add(new int[]{r, c});
- }
- }
- }
-
- int round = 0; // round 表示腐烂的轮数,或者分钟数
- while (count > 0 && !queue.isEmpty()) {
- round++;
- int n = queue.size();
- for (int i = 0; i < n; i++) {
- int[] orange = queue.poll();
- int r = orange[0];
- int c = orange[1];
- if (r-1 >= 0 && grid[r-1][c] == 1) {
- grid[r-1][c] = 2;
- count--;
- queue.add(new int[]{r-1, c});
- }
- if (r+1 < M && grid[r+1][c] == 1) {
- grid[r+1][c] = 2;
- count--;
- queue.add(new int[]{r+1, c});
- }
- if (c-1 >= 0 && grid[r][c-1] == 1) {
- grid[r][c-1] = 2;
- count--;
- queue.add(new int[]{r, c-1});
- }
- if (c+1 < N && grid[r][c+1] == 1) {
- grid[r][c+1] = 2;
- count--;
- queue.add(new int[]{r, c+1});
- }
- }
- }
-
- if (count > 0) {
- return -1;
- } else {
- return round;
- }
- }
- }
- class Solution {
- public int countPrimes(int n) {
- boolean[] isPrim = new boolean[n];
- Arrays.fill(isPrim, true);
- // 从 2 开始枚举到 sqrt(n)。
- for (int i = 2; i * i < n; i++) {
- // 如果当前是素数
- if (isPrim[i]) {
- // 就把从 i*i 开始,i 的所有倍数都设置为 false。
- for (int j = i * i; j < n; j+=i) {
- isPrim[j] = false;
- }
- }
- }
-
- // 计数
- int cnt = 0;
- for (int i = 2; i < n; i++) {
- if (isPrim[i]) {
- cnt++;
- }
- }
- return cnt;
- }
- }
- jimport java.util.*;
-
- public class Main {
-
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- int n = in.nextInt();
- String str = in.nextLine();
- String[] strs = str.split(" ");
- int r = in.nextInt();
- Set<Integer> set = new TreeSet();
- for(int i = 0; i < r; i++){
- set.add(in.nextInt());
- }
- List<String> res = new ArrayList();
- List<String> list = new ArrayList();
- for(Integer s : set){
- if(str.contains(String.valueOf(s))){
- res.add(String.valueOf(s));
- int count = 0;
- for(int i = 0;i < strs.length; i++){
- if(strs[i].contains(String.valueOf(s))){
- count++;
- list.add(String.valueOf(i - 1));
- list.add(strs[i]);
- }
- }
- res.add(String.valueOf(count));
- res.addAll(list);
- list.clear();
- }
- }
- System.out.print(res.size() + " ");
- for(String s1 : res){
- System.out.print(s1 + " ");
- }
- }
- }
- public class Solution {
- public int jumpFloor(int target) {
- if(target == 1 || target == 0){
- return 1;
- }
- if(target == 2){
- return 2;
- }
- return jumpFloor(target - 1) + jumpFloor(target - 2);
- }
- }
- import java.util.*;
- public class Solution {
- Map<Integer,Integer> map = new HashMap();
- public int jumpFloor(int target) {
- if(target == 1 || target == 0){
- return 1;
- }
- if(target == 2){
- return 2;
- }
- if(map.containsKey(target)){
- return map.get(target);
- } else{
- map.put(target,jumpFloor(target - 1) + jumpFloor(target - 2));
- return map.get(target);
- }
- }
- }
- import java.util.*;
- public class Solution {
- Map<Integer,Integer> map = new HashMap();
- public int jumpFloor(int target) {
- if(target == 1 && target == 2){
- return 1;
- }
- if(target == 3){
- return 2;
- }
- if(map.containsKey(target)){
- return map.get(target);
- } else{
- map.put(target,jumpFloor(target - 1) + jumpFloor(target - 3));
- return map.get(target);
- }
- }
- }
- import java.util.*;
-
- public class Main {
-
- public static void main(String[] args){
- Scanner in = new Scanner(System.in);
- String s = in.next();
- Map<Character,Integer> map = new HashMap();
- for(char c : s.toCharArray()){
- map.put(c,map.getOrDefault(c,1)+1);
- }
- int min = Integer.MAX_VALUE;
- for(int i : map.values()){
- min = Math.min(min,i);
- }
- for(char c : s.toCharArray()){
- if(map.get(c) == min){
- System.out.print(c);
- }
- }
- }
- }
- 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[][] nums = new int[n][n];
- for(int i = 0; i < n; i++){
- for(int j = 0; j < n; j++){
- nums[i][j] = in.nextInt();
- }
- }
- //对角线翻转
- for(int i = 0; i < n; i++){
- for(int j = 0; j < i; j++){
- nums[i][j] = nums[i][j] ^ nums[j][i];
- nums[j][i] = nums[j][i] ^ nums[i][j];
- nums[i][j] = nums[j][i] ^ nums[i][j];
- }
- }
- // 中轴线翻转
- for(int i = 0; i < n; i++){
- for(int j = 0; j < n/2; j++){
- nums[i][j] = nums[i][j] ^ nums[i][n-j-1];
- nums[i][n-j-1] = nums[i][n-j-1] ^ nums[i][j];
- nums[i][j] = nums[i][n-j-1] ^ nums[i][j];
- }
- }
-
- // 输出
- for(int i = 0; i < n; i++){
- for(int j = 0; j < n; j++)
- System.out.print(nums[i][j] + " ");
- System.out.println();
- }
- }
- }
- }
- import java.util.*;
-
- public class Solution {
- public int[][] rotateMatrix(int[][] mat, int n) {
- // 对角线对折
- for(int i = 0; i < n; i++){
- for(int j = 0; j < i; j++){
- mat[i][j] = mat[i][j] ^ mat[j][i];
- mat[j][i] = mat[i][j] ^ mat[j][i];
- mat[i][j] = mat[i][j] ^ mat[j][i];
- }
- }
- // 中轴线对折
- for(int i = 0; i < n; i++){
- for(int j = 0; j < n/2; j++){
- mat[i][j] = mat[i][j] ^ mat[i][n-j-1];
- mat[i][n-j-1] = mat[i][j] ^ mat[i][n-j-1];
- mat[i][j] = mat[i][j] ^ mat[i][n-j-1];
- }
- }
- return mat;
- }
- }
- /*
- public class ListNode {
- int val;
- ListNode next = null;
- ListNode(int val) {
- this.val = val;
- }
- }*/
- public class Solution {
- public ListNode ReverseList(ListNode head) {
- ListNode pre = null;
- ListNode next = null;
- while(head != null){
- next = head.next;
- head.next = pre;
- pre = head;
- head = next;
- }
- return pre;
- }
- }
int instance = Math.abs(x1-x2)+Math.abs(y1-y2);
- class Solution {
- public String longestCommonPrefix(String[] strs) {
- if(strs.length == 0) return "";
- String s = strs[0];
- for(String st : strs){
- while(!st.startsWith(s)){
- if(s.length() == 0) return "";
- s = s.substring(0,s.length()-1);
- }
- }
- return s;
- }
- }
- class Solution {
- public boolean canJump(int[] nums) {
- int n = 1;
- for(int i = nums.length - 2; i >= 0; i--){
- if(nums[i] >= n){
- n = 1;
- }else{
- n++;
- }
- if(i == 0 && n > 1){
- return false;
- }
- }
- return true;
- }
- }
- class Solution {
- public int jump(int[] nums) {
- int max = 0;
- int end = 0;
- int step = 0;
- for(int i = 0; i < nums.length - 1; i++){
- max = Math.max(max, nums[i] + i);
- if (i == end){
- end = max;
- step++;
- }
- }
- return step;
- }
- }
- class Solution {
- public int maxArea(int[] height) {
- int max = 0;
- for(int i = 0, j = height.length - 1; i < j;){
- int minHeight = height[i] < height[j] ? height[i++] : height[j--];
- max = Math.max(max,(j - i + 1) * minHeight);
- }
- return max;
- }
- }
- class Solution {
- public List<Integer> lexicalOrder(int n) {
- List<String> nums = new ArrayList();
- for(int i = 0; i < n; i++){
- nums.add(String.valueOf(i+1));
- }
- Collections.sort(nums);
- List<Integer> num = new ArrayList();
- for(int i = 0; i < n; i++){
- num.add(Integer.parseInt(nums.get(i)));
- }
- return num;
- }
- }
- class Solution {
- List<Integer> ans = new ArrayList<>();
- public List<Integer> lexicalOrder(int n) {
- for (int i = 1; i <= 9; i++) dfs(i, n);
- return ans;
- }
- void dfs(int cur, int limit) {
- if (cur > limit) return ;
- ans.add(cur);
- for (int i = 0; i <= 9; i++) dfs(cur * 10 + i, limit);
- }
- }
- class Solution {
- public List<Integer> lexicalOrder(int n) {
- List<Integer> ans = new ArrayList<>();
- for (int i = 0, j = 1; i < n; i++) {
- ans.add(j);
- if (j * 10 <= n) {
- j *= 10;
- } else {
- while (j % 10 == 9 || j + 1 > n) j /= 10;
- j++;
- }
- }
- return ans;
- }
- }
- class Solution {
- public int lastRemaining(int n) {
- int head = 1;
- int step = 1;
- boolean left = true;
-
- while (n > 1) {
- //从左边开始移除 or(从右边开始移除,数列总数为奇数)
- if (left || n % 2 != 0) {
- head += step;
- }
- step *= 2; //步长 * 2
- left = !left; //取反移除方向
- n /= 2; //总数 / 2
- }
-
- return head;
- }
- }
- class Solution {
- public boolean isPalindrome(String s) {
- StringBuilder sb = new StringBuilder();
- for(char c : s.toCharArray()){
- if(Character.isLetterOrDigit(c)){
- sb.append(Character.toLowerCase(c));
- }
- }
- String s_re = new StringBuilder(sb).reverse().toString();
- return sb.toString().equals(s_re);
- }
- }
- class Solution {
- public String longestPalindrome(String s) {
- int n = s.length();
- if(n < 2) return s;
- int start = 0, end = 0;
- for(int i = 0; i < n; i++){
- int max = Math.max(fun(s,i,i),fun(s,i,i+1));
- if(max > end - start){
- start = i - (max - 1) / 2;
- end = i + max / 2;
- }
- }
- return s.substring(start,end+1);
- }
-
- public int fun(String s,int begin,int end){
- while(begin >= 0 && end < s.length() && s.charAt(begin) == s.charAt(end)){
- begin--;
- end++;
- }
- return end - begin - 1;
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。