/**
* 1、编写代码完成如下功能
* public static String replace(String text, String target, String replace){
* ....
* }
* 示例:replace(“aabbccbb”, “bb”, “dd”); 结果:aadccdd
* 注意:不能使用String及StringBuffer等类的replace等现成的替换API方法。
*/
public class Test01 {
public static void main(String[] args) {
String str1 = "aabbccbb";
System.out.println(str1.length());
Scanner input = new Scanner(System.in);
System.out.println("请输入要被替换的字符串:");
String str2 = input.next();
System.out.println("请输入替换的值:");
String str3 = input.next();
System.out.println(replace(str1, str2, str3));
}
public static String replace(String text, String target, String replace){
String[] split = text.split(target);
String str = "";
for (int i = 0; i < split.length; i++) {
str+=(split[i]+replace);
}
return str;
}
}
2
/**
* 2、1个字符串中可能包含a-z中的多个字符,字符也可能重复,
* 例如:String data = “aabcexmkduyruieiopxzkkkkasdfjxjdsds”;
* 写一个程序,对于给定一个这样的字符串求出字符串出现次数最多的那个字母以及出现的次数
* (若次数最多的字母有多个,则全部求出)
*/
public class Test02 {
public static void main(String[] args) {
String data = "aabcexmkduyruieiopxzkkkkasdfjxjdsds";
char[] chars = data.toCharArray();
HashMap<Character, Integer> hashMap = new HashMap<>();
for (int i = 0; i < chars.length; i++) {
if (hashMap.containsKey(chars[i])){
hashMap.put(chars[i], hashMap.get(chars[i]) + 1);
}else {
hashMap.put(chars[i], 1);
}
}
Set<Map.Entry<Character, Integer>> entrySet = hashMap.entrySet();
int maxNum = 0;
for (Map.Entry<Character, Integer> entry : entrySet) {
if (maxNum < entry.getValue()){
maxNum = entry.getValue();
}
}
HashMap<Character, Integer> hashMap1 = new HashMap<>();
for (Map.Entry<Character, Integer> entry : entrySet) {
if (entry.getValue() == maxNum){
hashMap1.put(entry.getKey(), maxNum);
}
}
System.out.println(hashMap1.toString());
//System.out.println(findMaxValue(data));
}
3
/**
* 3、假设日期段用两个6位长度的正整数表示,例如:(201401,201406)用来表示2014年1月到2014年6月,
* 求两个日期段的重叠月份数。例如:输入:201401和201406,
* 201403和201409,输出:4
* 解释:重叠月份:3,4,5,6月共4个月
*/
public class Test03 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] nums = new int[4];
for (int i = 0; i < 4; i++) {
System.out.println("请循环输入年份:");
nums[i] = Integer.parseInt(scanner.next().substring(4));
//System.out.println(nums[i]);
}
if (nums[1] > nums[2] || nums[3] > nums[0]){
Arrays.sort(nums);
int max = nums[1] > nums[2] ? nums[1] :nums[2];
int min = nums[1] < nums[2] ? nums[1] :nums[2];
for (int i = min; i <= max; i++) {
System.out.println(i);
}
System.out.println(nums[2] - nums[1] + 1);
}
}
}
5
/**
* 5、编程实现单向链表,并实现单向链表的反转。比如一个链表是这样的:1->2->3->4->5,
* 通过反转后成为5->4->3->2->1,注:即实现单向链表类,在该类中提供一个单向链表的反转方法reverse,
* 请写出完整代码
*/
public class Test05 {
public static void main(String[] args) {
//构造链表
Node head = new Node(1);
Node node1 = new Node(2);
Node node2 = new Node(3);
Node node3 = new Node(4);
Node node4 = new Node(5);
head.next = node1;
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = null;
//打印反转前的链表
Node h = head;
while (h != null){
System.out.print(h.data + "->");
h = h.next;
}
System.out.println();
System.out.println("--------");
//反转链表
head = reverse(head);
//打印反转后的链表
while (head != null) {
System.out.print(head.data + "->");
head = head.next;
}
}
/**
* 反转单链表--遍历法
*
* @param head
* @return
*/
private static Node reverse(Node head){
// Node pre = head;
// Node cur = head.next;
// Node tmp = null;
// while (cur != null) {
// //缓存当前节点的指针域(即下一个节点)
// tmp = cur.next;
// //反转当前节点
// cur.next = pre;
// //后移节点
// pre = cur;
// cur = tmp;
// }
// //头结点的指针域置空
// head.next = null;
// return pre;
//更为高效的写法
if (head == null) return null;
Node node = head.next;
//将头结点置空
head.next = null;
while (node != null){
Node nextNode = node.next;
//反转当前节点
node.next = head;
//后移元素
head = node;
node = nextNode;
}
// Stack<Integer> stack = new Stack<>();
// Node tempHead = head;
// while (tempHead != null){
// stack.add(tempHead.data);
// tempHead = tempHead.next;
// //System.out.println(stack.pop());
// }
//
// Node tempHead01 = head;
// while (tempHead01 != null){
System.out.println(stack.pop());
// tempHead01.data = stack.pop();
// System.out.println(tempHead01.data);
// tempHead01 = tempHead01.next;
// }
// head = tempHead01;
return head;
}
static class Node{
int data;
Node next;
public Node(int data){
this.data = data;
}
}
6
//6、找出数组中一个值,使其左侧值的加和等于右侧值的加和,例如:1,2,5,3,2,4,2,结果为:第4个值
public class Test06 {
public static void main(String[] args) {
int[] arr = {1,2,5,3,2,4,2};
int i = stuFind(arr);
System.out.println(i);
// int index = 1;
// int sumLeft = arr[0];
// int sumRight = arr[arr.length - 1];
// for (int left = 0, right = arr.length - 1; left < right;) {
// if (sumLeft < sumRight){
// left++;
// sumLeft += arr[sumLeft];
// index++;
// }else if (sumLeft > sumRight){
// right--;
// sumRight += arr[sumRight];
// }else {
// break;
// }
// }
// System.out.println(index);
}
public static int stuFind(int[] array){
for(int i=1;i<array.length;i++){
int totalLeft=0;
for(int le=0;le<i;le++){
totalLeft+=array[le];
}
int totalRight=0;
for(int ri=i+1;ri<array.length;ri++){
totalRight+=array[ri];
}
if(totalLeft==totalRight){
return i;
}
}
return -1;
}
7
//7、编程实现:线程A向队列Q中不停写入数据,线程B从队列Q中不停读取数据(只要Q中有数据)
public class Test07 {
public static void main(String[] args) {
Stack<Integer> stack = new Stack<>();
new Read(stack).start();
new Write(stack).start();
}
}
class Read extends Thread{
private Stack stack;
public Read(Stack stack){
this.stack = stack;
}
@Override
public void run() {
while (true){
synchronized (Thread.class){
if (stack.empty()){
try {
System.out.println(Thread.currentThread().getName() + "栈为空,请存入数据!");
Thread.class.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
Object pop = stack.pop();
System.out.println(Thread.currentThread().getName() + "从栈中取出数据:" + pop);
}
}
}
}
class Write extends Thread{
private Stack stack;
public Write(Stack stack){
this.stack = stack;
}
@Override
public void run() {
while (true){
synchronized (Thread.class){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
Object push = stack.push(777);
System.out.println(Thread.currentThread().getName() + "向栈中放入数据:" + push);
Thread.class.notifyAll();
}
}
}
}
5
/**
* 5、编程实现单向链表,并实现单向链表的反转。比如一个链表是这样的:1->2->3->4->5,
* 通过反转后成为5->4->3->2->1,注:即实现单向链表类,在该类中提供一个单向链表的反转方法reverse,
* 请写出完整代码
*/
public class Test05 {
public static void main(String[] args) {
//构造链表
Node head = new Node(1);
Node node1 = new Node(2);
Node node2 = new Node(3);
Node node3 = new Node(4);
Node node4 = new Node(5);
head.next = node1;
node1.next = node2;
node2.next = node3;
node3.next = node4;
node4.next = null;
//打印反转前的链表
Node h = head;
while (h != null){
System.out.print(h.data + "->");
h = h.next;
}
System.out.println();
System.out.println("--------");
//反转链表
head = reverse(head);
//打印反转后的链表
while (head != null) {
System.out.print(head.data + "->");
head = head.next;
}
}
/**
* 反转单链表--遍历法
*
* @param head
* @return
*/
private static Node reverse(Node head){
// Node pre = head;
// Node cur = head.next;
// Node tmp = null;
// while (cur != null) {
// //缓存当前节点的指针域(即下一个节点)
// tmp = cur.next;
// //反转当前节点
// cur.next = pre;
// //后移节点
// pre = cur;
// cur = tmp;
// }
// //头结点的指针域置空
// head.next = null;
// return pre;
//更为高效的写法
if (head == null) return null;
Node node = head.next;
//将头结点置空
head.next = null;
while (node != null){
Node nextNode = node.next;
//反转当前节点
node.next = head;
//后移元素
head = node;
node = nextNode;
}
// Stack<Integer> stack = new Stack<>();
// Node tempHead = head;
// while (tempHead != null){
// stack.add(tempHead.data);
// tempHead = tempHead.next;
// //System.out.println(stack.pop());
// }
//
// Node tempHead01 = head;
// while (tempHead01 != null){
System.out.println(stack.pop());
// tempHead01.data = stack.pop();
// System.out.println(tempHead01.data);
// tempHead01 = tempHead01.next;
// }
// head = tempHead01;
return head;
}
static class Node{
int data;
Node next;
public Node(int data){
this.data = data;
}
}
/**
* 反转链表--递归法
*
* @param head
* @return
*/
// private static Node reverse(Node head) {
// if (head == null || head.next == null) {
// return head;
// }
// //reHead是反转后的链表的头结点
// Node reHead = reverse(head.next);
// //反转
// head.next.next = head;
// head.next = null;
// return reHead;
// }
}
749

被折叠的 条评论
为什么被折叠?



