1.给定一个字符串,按照字符与‘U’的差的绝对值大小升序排列
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String inputString = input.nextLine();
System.out.println(sort(inputString));
}
public static String sort(String str){
char[] ch = str.toCharArray();
boolean flag = true;
for(int i = 0; i < ch.length-1; i++){
for(int j = 0; j < ch.length-1-i; j++){
int m = ch[j] - 'U';
if(m < 0)
m = -m;
int n = ch[j+1] - 'U';
if(n < 0)
n = -n;
if(m > n){
char temp = ch[j];
ch[j] = ch[j+1];
ch[j+1] = temp;
flag = false;
}
}
if(flag)
break;
}
return new String(ch);
}
}
2.删除字符串中重复次数最少的字符,如abb-->bb aabb-->空串
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
import java.util.Set;
import java.util.List;
import java.util.ArrayList;
public class RemoveDuplicateLeastChar{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
String inputString = input.nextLine();
System.out.println(remove(inputString));
}
public static String remove(String str){
Map<Character, Integer> map = new HashMap<Character, Integer>();
char [] ch = str.toCharArray();
for(char c : ch){
if(map.containsKey(c))
map.put(c, map.get(c) + 1);
else
map.put(c, 1);
}
int least = 128;
Set<Character> set = map.keySet();
for(Character cha : set) {
if(map.get(cha) < least)
least = map.get(cha);
}
List<Character> list = new ArrayList<Character> ();
for(Character cha : set) {
if(map.get(cha) == least){
for(int j = 0; j< ch.length; j++){
if(ch[j] == cha)
list.add(ch[j]);
}
}
}
StringBuffer sb = new StringBuffer();
for(char c : ch){
if(!list.contains(c))
sb.append(c);
}
return sb.toString();
}
}
3.给出二叉树中和为某一值的所有路径
import java.util.Scanner;
import java.util.ArrayList;
/*
binarytree:
10
5 12
4 7
*/
class Node{
int data;
Node left;
Node right;
}
public class AllSumTreePath{
private Node root;
public void insert(int key){
Node node = new Node();
node.data = key;
if(root == null)
root = node;
else{
Node current = root;
Node parent = null;
while(true){
parent = current;
if(key <= current.data){
current = current.left;
if(current == null){
parent.left = node;
return;
}
}
else{
current = current.right;
if(current == null){
parent.right = node;
return;
}
}
}
}
}
public static void main(String[] args) {
AllSumTreePath tree = new AllSumTreePath();
Scanner input = new Scanner(System.in);
int sum = input.nextInt();
Scanner input1 = new Scanner(System.in);
String inputString = input1.nextLine();
String [] strarray = inputString.split(",");
for(String val : strarray){
int data = Integer.parseInt(val);
tree.insert(data);
}
// tree.insert(10);
// tree.insert(5);
// tree.insert(12);
// tree.insert(4);
// tree.insert(7);
// int sum = 22;
solution(tree.root, sum, new ArrayList<Integer>());
}
public static void solution(Node node, int sum, ArrayList<Integer> path){
if (node == null)
return;
path.add(node.data);
if(node.left == null && node.right == null) {
int pathNum = 0;
for(int value : path){
pathNum = pathNum + value;
}
if(pathNum == sum){
boolean isFirst = true;
for(int value : path){
if(isFirst){
System.out.print(value);
isFirst = false;
}else
System.out.print("," + value);
}
System.out.println();
}
}
solution(node.left, sum, path);
solution(node.right, sum, path);
path.remove(path.size() - 1);
}
}