算法题分类

一、字符串

//String 转 int、long、double(同理),注意int的取值范围
String s = "1234567899";
int i = Integer.parseInt(s);

//int 转 String,三种方法
int i = 12345;
String str1 = i + "";
String str2 = String.valueOf(i);
String str3 = Integer.toString(i);

//char 转 int 的两种方法
char ch = '9';
int i1 = ch - '0';
int i2 = Character.getNumericValue(ch);

例1:找出输入的字符串中出现次数最多的字符及次数

import java.util.HashMap;
import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine().replace(" ", "");
		System.out.println(str);
		
		HashMap<Character, Integer> m = new HashMap<>();
		char ch,ch2 = ' ';
		int max = 1,temp;
		for(int i = 0; i < str.length(); i++) {
			ch = str.charAt(i);
			if(m.containsKey(ch)) {
				temp = m.get(ch)+1;
				m.put(ch, temp);
				if(temp > max) {
					max = temp;
					ch2 = ch;
				}
			}else {
				m.put(ch, 1);
			}
		}
		System.out.println("出现最多次数的字符是:"+ch2+",出现了:"+max+"次!");
	}		
}

例2:找出字符串中连续出现次数最多的字符及次数

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String str = sc.nextLine().replace(" ", "");
		int max = 1,count = 1;
		char ch = str.charAt(0),temp;
		for(int i = 1; i < str.length(); i++) {
			temp = str.charAt(i);
			if(str.charAt(i-1) == temp) {
				count += 1;
				if(count > max) {
					ch = temp;
					max = count;
				}
			}else {
				count = 1;
			}
		}
		System.out.println("连续出现次数最多的字符是:"+ch+",出现了:"+max+"次!");
	}		
}

例3:打印账单,阿拉伯数字转中文

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
		int len = s.length()-1;
		char[] ch = {'零','壹','贰','叁','肆','伍','陆','柒','捌','玖'};
		char[] ch2 = {'元','拾','佰','仟','万'};
		StringBuffer st = new StringBuffer();
		int i = 0;
		int temp,temp2=1;
		while(len >= 0) {
			temp = s.charAt(i++)-'0';
			if(temp == 0 && temp == temp2) {
				;
			}else {
				st.append(ch[temp]);
			}
			temp2 = temp;
			if(temp == 0) {
				len--;
				continue;
			}else {
				st.append(ch2[len--]);
			}
		}
		st.append("整");
		System.out.println(st);
	}		
}

 

 

 

二、二叉树

例1:给出先序、中序,求后序;

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.nextLine();
		String[] s2 = s.split(",");
		char[] preOrder = s2[0].toCharArray();
		char[] inOrder = s2[1].toCharArray();
		TreeNode root = buildBiTree(preOrder, inOrder);
		posOrder(root);
	}
	
	public static TreeNode buildBiTree(char[] preOrder, char[] inOrder) {
		int size = preOrder.length;
		if(size<=0) {
			return null;
		}else {
			return preInOrder(preOrder, inOrder, 0, size-1, 0, size-1);
		}
		
	}
	
	private static TreeNode preInOrder(char[] preOrder, char[] inOrder, int i, int j, int k,int h) {
		TreeNode root = new TreeNode();
		root.setData(preOrder[i]);
		int m = k;
		while(inOrder[m] != preOrder[i]) {
			m++;
		}
		if(m == k) {
			root.setlChild(null);
		}else {
			root.setlChild(preInOrder(preOrder,inOrder, i+1,i+m-k,k,m-1));
		}
		if(m == h) {
			root.setrChild(null);
		}else {
			root.setrChild(preInOrder(preOrder,inOrder,i+m-k+1, j, m+1, h));
		}
		return root;
	}
	
	public static void posOrder(TreeNode root) {
		if(root == null) return;
		posOrder(root.getlChild());
		posOrder(root.getrChild());
		System.out.print(root.getData());
	}
}

class TreeNode{
	char data;
	TreeNode lChild;
	TreeNode rChild;
	public char getData() {
		return data;
	}
	public void setData(char data) {
		this.data = data;
	}
	public TreeNode getlChild() {
		return lChild;
	}
	public void setlChild(TreeNode lChild) {
		this.lChild = lChild;
	}
	public TreeNode getrChild() {
		return rChild;
	}
	public void setrChild(TreeNode rChild) {
		this.rChild = rChild;
	}
}

三、链表

例1:每k个长度的链表翻转,最后不够k个的不翻转

import java.util.Scanner;
public class Main {
	static Node first;
	static Node temp;
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		String s = sc.next();
		int k = sc.nextInt();
		int len = s.length();
		for(int i = 0; i < len; i++) {
			addLast(s.charAt(i));
		}
		temp = first;
		for(int j = 0; j < len/k; j++) {
			for(int i = 0; i < k; i++) {
				if(i == 0) {
					reverse(temp);
				}
				temp = temp.next;
			}
		}
	}
	
	public static void addLast(char ch) {
		Node newNode = new Node(ch, null);
		if(first == null) {
			first = newNode;
			temp = first;
		}else {
			temp.next = newNode;
		}
		temp = newNode;
	}
	
	public static void reverse(Node node) {
		
	}
}

class Node{
	char data;
	Node next;
	
	Node(char data, Node next){
		this.data = data;
		this.next = next;
	}
}

 

 

 


四、求阶乘的末尾有几个0

import java.util.Scanner;
public class Main {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int n = sc.nextInt();
		int count = fun(n);
		System.out.println(count);	
	}
	
	public static int fun(int n) {
		int i,sum = 0;
		for(; n > 0; n--) {
			i = n;
			while(true) {
				if(i%5 == 0) {
					sum++;
					i = i/5;
				}else {
					break;
				}
			}
		}
		return sum;
	}
}

 

 

 

 

 

KMP算法

算法思想:分析子串,找出子串的特点,对于子串中有与首字符相等的字符,可以省掉一部分不必要的比较,主串的i指针也可以不回溯。

主串Si 和 子串Tj 匹配,若某趟匹配失败后,如果模式串中有满足  

 

 

 


输入一个日期,输出最近6个月

public class Main {
    public static void main(String[] args) throws ParseException {
        System.out.println("请输入yyyy-MM格式的日期");
        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();	//输入一个日期,如“2019-09”
        Pattern p = Pattern.compile("\\d{4}+[-]\\d{1,2}+");     //用正则表达式测试输入格式是否正确
        Matcher m = p.matcher(str);
        if(!m.matches()){
            System.out.println("1.输入不合法,请重新运行、输入!!!");
            return;
        }

        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM");	//用于字符串和日期格式转换
        Calendar c = Calendar.getInstance();
        int curYear = c.get(Calendar.YEAR);     //获取当前年
        int curMon = c.get(Calendar.MONTH)+1;   //获取当前月
        int year = Integer.parseInt(str.substring(0, 4));	//取出年份,截取字符串并转成int类型
        int month = Integer.parseInt(str.substring(5, 7));	//取出月份,截取字符串并转成int类型
        if(year <= 0 || year > curYear || month <= 0 || month > 12
                || (year == curYear && month > curMon)){
            System.out.println("2.输入不合法,请重新运行、输入!!!");
            return;
        }

        ArrayList<String> output = new ArrayList<String>();		//用来存储输出的前6个月
        for(int i = 0; i < 6; i++) {	//计算出最近的6个月
            if(month < 1) {		//当月份小于1时,年数到上一年
                year = year - 1;
                month = 12;
            }
            str = year + "-" + month--;
            output.add(ft.format(ft.parse(str)));	//先把字符串转化成日期格式,再转化成字符串存到集合中
        }
        for (String string : output) {		//遍历输出最近6个月
            System.out.println(string);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值