赞
踩
---hashMap --- 按value排序
- // map排序
- public static void sortMap(Map<String,Integer> map){
- List<Map.Entry<String,Integer>> list = new ArrayList<>(map.entrySet());
- // list.sort
- list.sort(new Comparator<Map.Entry<String, Integer>>() {
- @Override
- public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
- return o2.getValue().compareTo(o1.getValue());
- }
- });
- // Collections.sort
- Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
- @Override
- public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
- return o2.getValue().compareTo(o1.getValue());
- }
- });
- for(Map.Entry<String,Integer>entry :list){
- System.out.println(entry.getKey()+" "+entry.getValue());
- }
-
- }

-----骰子翻转----
骰子有6个面,现在用1,2,3,4,5,6分别代表一个骰子的左,右,前,后,上,下的初始位置,用R代表向右滚动一次,用L代表向左滚动一次,可以向前翻转(用F表示向前翻转1次),可以向后翻转(用B表示向右翻转1次),可以逆时针旋转(用A表示逆时针旋转90度),可以顺时针旋转(用C表示逆时针旋转90度),现从初始状态开始,根据输入的动作序列,计算得到最终的状态。
-
-
- import com.alibaba.fastjson.JSONObject;
-
- // 筛子翻转
- public class Shaizifanzhuan {
- // 数组排序
- public static void main(String[] args) {
- int[] arrs= {0,1,2,3,4,5};
- System.out.println("-"+JSONObject.toJSONString(arrs));
- System.out.println(JSONObject.toJSONString(paixu(arrs,"R")));
-
- }
-
- public static int[] paixu (int[] nums,String str){
- // nums = [0,1,2,3,4,5] 左右前后上下
- int[] result = new int[6];
- switch (str){
- case "R": // 右
- result[0]=nums[5];
- result[1]=nums[4];
- result[2]=nums[2];
- result[3]=nums[3];
- result[4]=nums[2];
- result[5]=nums[0];
- break;
- case "L": // [0,1,2,3,4,5] 左右前后上下
- result[0]=nums[4];
- result[1]=nums[5];
- result[2]=nums[2];
- result[3]=nums[3];
- result[4]=nums[1];
- result[5]=nums[0];
- break;
-
- case "F": //前 // nums = [1,2,3,4,5,6] 左右前后上下
- result[0]=nums[0];
- result[1]=nums[1];
- result[2]=nums[4];
- result[3]=nums[5];
- result[4]=nums[3];
- result[5]=nums[2];
-
- break;
- case "B": // 后 [0,1,2,3,4,5] 左右前后上下
- result[0]=nums[0];
- result[1]=nums[1];
- result[2]=nums[5];
- result[3]=nums[4];
- result[4]=nums[2];
- result[5]=nums[3];
- break;
- case "A": //逆时针 [0,1,2,3,4,5] 左右前后上下
- result[0]=nums[3];
- result[1]=nums[2];
- result[2]=nums[0];
- result[3]=nums[1];
- result[4]=nums[4];
- result[5]=nums[5];
- break;
-
- case "C": // [0,1,2,3,4,5] 左右前后上下
- result[0]=nums[2];
- result[1]=nums[3];
- result[2]=nums[1];
- result[3]=nums[0];
- result[4]=nums[4];
- result[5]=nums[5];
- break;
- }
-
-
- return result;
- }
-
- }

-----判断牌型-------
五张牌,每张牌由牌大小和花色组成,牌大小2~10、J、Q、K、A,牌花色为红桃、黑桃、梅花、方块四种花色之一。 判断牌型:
* 牌型1,同花顺:同一花色的顺子,如红桃2红桃3红桃4红桃5红桃6。
* 牌型2,四条:四张相同数字 + 单张,如红桃A黑桃A梅花A方块A + 黑桃K。
* 牌型3,葫芦:三张相同数字 + 一对,如红桃5黑桃5梅花5 + 方块9梅花9。
* 牌型4,同花:同一花色,如方块3方块7方块10方块J方块Q。
* 牌型5,顺子:花色不一样的顺子,如红桃2黑桃3红桃4红桃5方块6。
* 牌型6,三条:三张相同 + 两张单。
* 牌型7,其他。
* 说明:
* 1)五张牌里不会出现牌大小和花色完全相同的牌。
* 2)前面的牌型比后面的牌型大,如同花顺比四条大,依次类推。
*/
-
-
- import org.apache.ibatis.annotations.Case;
-
- import java.util.*;
-
- // 判断牌型
- public class Panduanpaixing {
-
-
- public static void main(String[] args) {
- ArrayList<String> list = new ArrayList<>();
- list.add("1 红");
- list.add("2 a");
- list.add("2 b");
- list.add("2 c");
- list.add("5 红");
- System.out.println(getType(list));
-
- }
-
- // J 红 , 1 黑 ,J 红 , 1 黑 ,J 红 ,
- public static int getType(ArrayList<String> list){
- int type = 0;
- List<Integer> listNumber = new ArrayList<>();
- List<String> listColor = new ArrayList<>();
- Set<Integer> setNum = new TreeSet<>();
- Set<String> setColor = new TreeSet<>(); // 颜色相同 一个值
- Map<String,Integer> map = new HashMap<>(); // 数字 数显的频率
- list.stream().forEach(str->{
- String [] strs = str.split(" ");
- String strNumber = strs[0] ;
- String strColor = strs[1];
- if(map.containsKey(strNumber)){
- map.put(strNumber,map.get(strNumber)+1);
- }else{
- map.put(strNumber,1);
- }
- switch (strNumber){
- case "J":
- listNumber.add(11);
- setNum.add(11);
- break;
- case "Q":
- listNumber.add(12);
- setNum.add(12);
- break;
- case "K":
- listNumber.add(13);
- setNum.add(13);
- break;
- case "A":
- listNumber.add(1);
- setNum.add(1);
- break;
- default:
- listNumber.add(Integer.parseInt(strNumber));
- setNum.add(Integer.parseInt(strNumber));
- }
- listColor.add(strColor);
- setColor.add(strColor);
- });
- // 判断同花 4
- if(setColor.size()==1){ //同花
- //判断同花顺 1
- if(setNum.size()==5 ){
-
- if(isShun(listNumber)){
- type = 1; // 同花顺
- }
- }else{
- type = 4; //同花
- }
-
- }
- // 顺子 5
- if(setColor.size()!=1){
- if(setNum.size()==5){
- if(isShun(listNumber)){
- type = 5;
- }
- }
- }
- // 三条6 四条2 葫芦3
- if(setNum.size()==3){ // 22 33 1; 333 1 2
- if(map.containsValue(3)){
- type = 6;
- }
- }
- if(setNum.size()==2){ // 1111 2; 11 333
- if(map.containsValue(3)){
- type = 3;
- }
- if(map.containsValue(4)){
- type = 2;
- }
- }
-
-
-
-
- return type;
- }
-
- public static boolean isShun(List<Integer> listNumber){ // 顺子
-
- boolean isThs = true;
- for(int i =1;i<5;i++){
- if(listNumber.get(i)!=listNumber.get(i-1)+1){
- isThs = false;
- }
-
- }
- return isThs;
- }
-
- }

-----按照字母个数排序-------
给定长度小于100的一个字符串,区分大小写的情况下统计其中字母的个数,并按照由大到小的顺序输出.当字母个数一样时,按照把先出现的字母放在前面.当字符串中没有字母时,则不要输出.
举例:
输入:
abbbdcdc123
输出:
b=3
d=2
c=2
a=1
- package utils.huawei;
-
- import com.alibaba.fastjson.JSONObject;
-
- import java.util.*;
-
- // 最多字母次数 排序输出
- public class Zuigaocishuzimu {
-
- public static void main(String[] args) {
-
- sort("aabbbdcdc123");
- }
-
- public static void sort(String str){ //abbbdcdc123
- Map<Character,Integer> map = new LinkedHashMap<>();
- Set<Character> set = new HashSet<>();
-
- // hashMap根据 values排序
- for(char cha : str.toCharArray()){
- if(cha>='A'&&cha<='z'){
- if(map.containsKey(cha)){
- map.put(cha,map.get(cha)+1) ; // a1 b3 d2 c2
- }else {
- map.put(cha, 1);
- }
-
- // set.toArray();
- }
- }
- System.out.println(JSONObject.toJSONString(map));
- char [] chars = new char[map.size()];
- int [] nums = new int[map.size()];
- int i =0;
- Object o = map.keySet().toArray()[0];
- Object a = map.values().toArray()[0];
- chars[0] = (char)o;
- nums[0] = (int) a;
-
-
- for(Map.Entry<Character,Integer> entry: map.entrySet()){
- char ch = entry.getKey();
- Integer num= entry.getValue();
- chars[i]=ch; //a
- nums[i]=num; //1 根据num排序 3 i-1:2
- int j = i; // 0
-
- while( j>0&&nums[j]>nums[j-1]){ // 交换j 到合适的地方
- int tempNum = nums[j];
- char tempChar = chars[j];
- chars[j]= chars[j-1];
- chars[j-1] = tempChar;
- nums[j] =nums[j-1];
- nums[j-1] = tempNum;
-
- }
- i++;
- }
-
- System.out.println(chars);
-
- for(int s =0;s<=chars.length-1;s++){
- System.out.println(chars[s]+" "+nums[s]);
- }
-
- }
-
-
- // map排序
- public static void sortMap(Map<String,Integer> map){
- List<Map.Entry<String,Integer>> list = new ArrayList<>(map.entrySet());
- // list.sort
- list.sort(new Comparator<Map.Entry<String, Integer>>() {
- @Override
- public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
- return o2.getValue().compareTo(o1.getValue());
- }
- });
- // Collections.sort
- Collections.sort(list, new Comparator<Map.Entry<String, Integer>>() {
- @Override
- public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
- return o2.getValue().compareTo(o1.getValue());
- }
- });
- for(Map.Entry<String,Integer>entry :list){
- System.out.println(entry.getKey()+" "+entry.getValue());
- }
-
- }
- }
-
-

--------消除重复数字的最大整数------------------
-
-
- import java.math.BigInteger;
- import java.util.Arrays;
- import java.util.HashMap;
- import java.util.Map;
-
- // 消除重复数字的最大整数
- public class MostBig {
-
- public static void main(String[] args) {
- mostBig(new BigInteger("2134123745971246890123064"));
- }
-
- public static void mostBig(BigInteger data){
- String str = data+"";
- char[] chars = str.toCharArray();
- Arrays.sort(chars);
- Map<String,Integer> map = new HashMap<>();
- for(int i=chars.length-1;i>=0;i--){
- char cha = chars[i];
- if(!map.containsKey(cha+"")){
- System.out.print(cha);
- map.put(cha+"",1);
- }
- }
-
- }
- }

-------------------牛客网小题汇总-------
-
- import com.alibaba.fastjson.JSONObject;
-
- import java.util.*;
-
- public class Main {
-
- // 子串的最长路径查找
- public static void zichuanlujing(String [] strs){
- Arrays.sort(strs);
- Arrays.stream(strs).forEach(str-> System.out.println(str));
- }
-
- public static void main(String [] args){
-
- Scanner scanner = new Scanner(System.in);
- int num = scanner.nextInt();
- while(scanner.hasNext()){
-
- String [] strs = new String[num];
- for(int i =0;i<num;i++){
- strs[i] = scanner.next();
- }
- System.out.println(JSONObject.toJSONString(strs));
- zichuanlujing(strs);
- }
-
-
- // 字符串反转
- // Scanner scanner = new Scanner(System.in);
- // String str = scanner.nextLine();
- // char [] strs = str.toCharArray();
- // StringBuffer stringBuffer = new StringBuffer();
- // for(int i=0;i<strs.length;i++){
- // stringBuffer.append(strs[strs.length-i-1]);
- // }
- // System.out.println(stringBuffer);
-
- //合并表记录
- // Scanner scanner = new Scanner(System.in);
- // Map<Integer,Integer> map = new TreeMap<>();
- // while(scanner.hasNext()){
- // int lenth = scanner.nextInt();
- // while(lenth-->0){
- //
- // int key = scanner.nextInt();
- // int value = scanner.nextInt();
- // if(map.containsKey(key)){
- // map.put(key,value+map.get(key));
- // }else{
- // map.put(key,value);
- // }
- // }
- // for(Map.Entry<Integer,Integer> entry :map.entrySet()){
- // System.out.println(entry.getKey()+" "+entry.getValue());
- // }
- // }
-
-
-
- // System.out.println(isZhishu(3));
-
- // while(scanner.hasNext()){
- // String str1 = scanner.nextLine().replaceAll(" ","");
- // String str2 = scanner.nextLine().replaceAll(" ","");
- // ArrayList a1 = strSplit(str1);
- // ArrayList a2 = strSplit(str2);
- // a1.stream().forEach(str-> System.out.println(str));
- // a2.stream().forEach(str-> System.out.println(str));
- // }
-
- }
-
- //句子反转
- public static String reserse(String str){
- String []strs = str.split(" ");
- StringBuffer result = new StringBuffer();
- for(int i =strs.length-1;i>=0;i--){
- if(i==0){
- result.append(strs[i]);
- }else{
- result.append(strs[i]+" ");
- }
- }
- return result.toString();
- }
-
- // 数字颠倒
- public static void shuzidiandao(int data){
- char[] chars = (data+"").toCharArray();
- for(int i=chars.length-1;i>=0;i--){
- System.out.print(chars[i]);
- }
-
- }
- // 重复字符最长串 aaaaabbbbaaccccc
- public static String maxRepeat(String str){
- if(str.length()==1){
- return str;
- }
- char[] chars = str.toCharArray();
- String temp = str.substring(0,1);
-
- int start = 1;
- int end = 1;
- Map<Integer,Integer> map = new HashMap<>(); // key 开始,value 结束
- for(int i=1;i<chars.length;i++){ // 分割数组
- if(chars[i]!=chars[i-1]){ // 不相等分割
- // end = i;
- if((end-start)>temp.length()||(end-start)==temp.length()&&temp.charAt(0)>chars[i]){ // 长度小于 或者accis吗小于
- temp = str.substring(start,end);
- }
- if(temp.length()==1){ // 单独 a b c
- if(chars[i]<chars[i-1]){
- temp = chars[i]+"";
- }
- }
- start = i;
- end = i;
- }else{
- end = i;
- // 判断是最后一个串, 则需要重新判断temp
- if(i==chars.length-1){
- String s = str.substring(start,end);
- System.out.println("-"+s);
- if(s.length()>temp.length()||s.charAt(0)<temp.charAt(0)){
- temp = s;
-
- }
- }
- }
- }
-
- return temp;
- }
-
- // 重复字符排序 ABCDAB -> AB
- public static void chongfuzifupaixu(String str){
- Map<Character ,Integer> map = new TreeMap<>();
- char [] chars = str.toCharArray();
- for(int i=0;i<chars.length;i++){
- if(map.containsKey(chars[i])){
- map.put(chars[i],map.get(chars[i])+1);
- }else{
- map.put(chars[i],1);
- }
- }
- for(Map.Entry<Character,Integer> entry:map.entrySet()){
- if(entry.getValue()>1){
- System.out.print(entry.getKey());
- }
- }
- }
-
- // 计算字符个数
- public static int zifugeshu(String str){
- char [] chars = str.toCharArray();
- Set<Character> set = new HashSet<>();
- for(int i=0;i<chars.length;i++){
- if(chars[i]>=0&&chars[i]<=127){
- set.add(chars[i]);
- }
- }
- return set.size();
- }
-
- // 提取不重复的整数
- public static void getbuchongfu(int data){
- Set<String> set = new TreeSet();
- String str = data+"";
- for(int i=str.length()-1;i>=0;i--){
- if(!set.contains(""+str.charAt(i))){
- set.add(""+str.charAt(i));
- System.out.print(str.charAt(i));
- }
- }
-
- }
-
- // 链表操作
-
- // 二叉树 先序遍历 非递归
- public static void treeBianli(TreeNode treeNode){
- Stack<TreeNode> stack = new Stack<>();
- while(treeNode!=null||!stack.isEmpty()){
-
- while(treeNode.left!=null){
- System.out.println(treeNode.val);
- stack.push(treeNode);
- treeNode = treeNode.left;
- }
- if(stack!=null){
- treeNode = stack.pop();
- treeNode = treeNode.right;
- }
- }
-
- }
-
- // 二叉树遍历 递归
- public static void xianxuBian(TreeNode treeNode){
- System.out.println(treeNode.val);
- if(treeNode.left!=null){
- xianxuBian(treeNode.left);
- }
- if(treeNode.right!=null){
- xianxuBian(treeNode.right);
- }
- }
-
- // 快排序
- public static void quickSort(int []numbers,int start,int end ){
- if(start<end){
- int base = numbers[start];// 基数
- int temp ;
- int i = start ;
- int j =end;
- do{
- while(numbers[i]>=base&&i<end){
- i++;
- }
- while(numbers[j]<=base&& j>start){
- j--;
- }
- // 交换
- if(i<=j){
- temp = numbers[i];
- numbers[i] = numbers[j];
- numbers[j] = temp;
- }
- }while(i<j); // 找到base 位置, 左边比它小,右边比它大
-
- numbers[start] = numbers[i]; // base位置变换
- numbers[i] = base;
- quickSort(numbers,start,i-1);
- quickSort(numbers,i+1,end);
-
- }
- }
-
- // 取近似值
- public static int getJinshizhi(float data){
- String str = new String (data+"");
- String[] strs = str.split(".");
- if(strs.length==2){
- if(Integer.parseInt(strs[1])>=5){
- return Integer.parseInt(strs[0])+1;
- }
- }
- return Integer.parseInt(strs[0]);
- }
- public static int getJinshizhi2(float f){
- int i = (int)f;
- i = (f-i)>=0.5?++i:i;
- return i;
- }
-
- // 质数因子
- public static String getZhishuResult(long data){
- StringBuffer result = new StringBuffer("");
- int i = 2;
- while(data-->0){
- if(data%i==0&&isZhishu(i)){ //因数
- result.append(i+" ");
- i++;
- }
- }
- return result.toString();
- }
-
- // 质数因子2
- public static void zhishuyinzi(long data){
- while(data!=1){
- for(int i =2;i<data;i++){
- if(data%i==0){
- System.out.println(i+" ");
- data =data/i;
- break;
- }
- }
- }
- }
-
- public static void canculate(long num){
- for(long i=2;i<=num;i++){
- if(num%i==0){
- System.out.print(i+" ");
- canculate(num/i);
- return;
- }
- }
- }
-
- // 判断是否为质数
- public static boolean isZhishu(long data){
- boolean result = true;
- int i =2;
- while(data-->0&&i<data){
- if( data%(i++)==0){
- result = false;
- break;
- }
- }
- return result;
- }
-
- // 输入十六进制 -》 十进制
- public static int shiliu(String str){
-
- return Integer.parseInt(str.substring(2),16);
- }
-
- // 字符串分割
- public static ArrayList<String> strSplit(String str){
- ArrayList<String> arrayList = new ArrayList<>();
- while(str.length()>=8){
- arrayList.add(str.substring(0,8));
- str = str.substring(8);
- }
- StringBuffer last = new StringBuffer(str);
- for(int i=0;i<8- str.length();i++){
- last.append("0");
- }
- arrayList.add(last.toString());
- // arrayList.toArray(new String [0]); 返回数组
- return arrayList;
- }
-
- // 数组去重排序
- public static int[] quchongpaixu (int [] arr){
- // 排序
-
- for(int i=0;i<arr.length-1;i++){
- boolean isSorted = true;
- for(int j =0;j<arr.length-1-i;j++){
- if(arr[j]>arr[j+1]){
- int temp = arr[j];
- arr[j] = arr[j+1];
- arr[j+1] = temp;
- isSorted = false;
- }
- }
- if(isSorted){
- break;
- }
- }
-
- // 去重
- ArrayList<Integer> arrayList = new ArrayList();
- arrayList.add(arr[0]);
- for(int i:arr){
-
- if(arrayList.get(arrayList.size()-1)!=i){
- arrayList.add(i);
- }
- }
- Integer[] result = new Integer[arrayList.size()];
- return arrayList.stream().mapToInt(Integer::intValue).toArray();
- }
-
- // 数组包含
- public static int numHas(String str1,String target){
- int num =0;
- for(char ch : str1.toCharArray()){
- if(ch==target.charAt(0)|| ch== target.toUpperCase().charAt(0)||ch == target.toLowerCase().charAt(0)){
- num++;
- }
- }
- return num;
- }
-
- public static int lent(String str){
- String [] strs = str.split(" ");
- return strs[strs.length-1].length();
- }
- }
- class Node{
- String str ;
- Node next ;
-
- public Node getNext() {
- return next;
- }
-
- public String getStr() {
- return str;
- }
-
- public void setNext(Node next) {
- this.next = next;
- }
-
- public void setStr(String str) {
- this.str = str;
- }
- }
- class TreeNode{
- int val;
- TreeNode left;
- TreeNode right;
- }

--------3水瓶子换一瓶水-----------
-
- import java.util.Scanner;
-
- public class Qishiping {
- int sum =0;
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
-
-
- while(scanner.hasNext()){
- int in = scanner.nextInt();
- if(in==0){
- break;
- }
-
- if(in==1){
- System.out.println(0);
- }
- if(in==2){
- System.out.println(1);
- }
- System.out.println(h(in));
- // int c = in%3; //
- // if(in%3==2){ // 喝完,剩下2个 +1
- // int b = (int)in/3;
- // sum+=b; //
- // (b+c)%3
- // }
- }
- }
- static int h(int n){
- if(n<2){
- return 0;
- }
- int a = n%3; // 59/3 19 2
- if(n==2){
- return 1; // 借一瓶
- }
- int b = n/3; // 19
-
- return b+h(b+a);
- }
-
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。