2019阿里巴巴编程测试题(在线和手写,记事本写)~~
要求:
给定一个字符串,按照给定的字节数要求输出,
如: str="ABCDEF", 输出4个字节,则为: ABCD.
如: str="哈ABCDEF",输出4个字节,则应为 “哈AB”。
我写的代码是这样的:
@Test
public void test1() {
String str="哈abcdf";
System.out.println(outputByByteNum(str,4));
}
/**
* @Title: outputByByteNum
* @Description: 按字节要求输出字符串
* @param: @param str 要求的字符串
* @param: @param num 要求输出的字节数
* @return: String
*/
public static String outputByByteNum(String str ,int num){
char[] ch=str.toCharArray();
int count=0;
StringBuilder sb=new StringBuilder();
for(char c:ch){
if((int)c>255 && count<num)
count+=2;
else if((int)c<255 && count<num)
count+=1;
else
break;
sb.append(c);
}
return sb.toString();
}
希望各位大佬,给一种另外的判断字节数的方式。 谢谢。我这个是根据asc码来判断的。有点牵强
------
用非递归实现一个树的前序遍历;
public class Node {
private int num;
private Node left;
private Node right;
public Node Node(){
Node J = new Node(8, null, null);
Node H = new Node(4, null, null);
Node G = new Node(2, null, null);
Node F = new Node(7, null, J);
Node E = new Node(5, H, null);
Node D = new Node(1, null, G);
Node C = new Node(9, F, null);
Node B = new Node(3, D, E);
Node A = new Node(6, B, C);
return A;
}
public Node(){}
public Node(int num, Node left, Node right) {
super();
this.num = num;
this.left = left;
this.right = right;
}
public int getNum() {
return num;
}
public Node getLeft() {
return left;
}
public Node getRight() {
return right;
}
@Override
public String toString() {
return "Node [num=" + num + ", left=" + left + ", right=" + right + "]";
}
}
public class Main {
public static void main(String[] args) {
Node n=new Node().Node();
posOrder1(n);
}
//递归前
public static void preFirst(Node A){
if(A!=null){
System.out.println(A);
preFirst(A.getLeft());
preFirst(A.getRight());
}
}
//递归中
public static void midorder(Node A){
if(A!=null){
midorder(A.getLeft());
System.out.println(A);
midorder(A.getRight());
}
}
//递归后
public static void posOrder(Node A){
if(A!=null){
posOrder(A.getLeft());
posOrder(A.getRight());
System.out.println(A);
}
}
//非递归的前序遍历
public static void preOrder1(Node Node)
{
Stack<Node> stack = new Stack<Node>();
while(Node != null || !stack.empty())
{
while(Node != null)
{
System.out.println(Node + " ");
stack.push(Node);
Node = Node.getLeft();
}
if(!stack.empty())
{
Node = stack.pop();
Node = Node.getRight();
}
}
}
//非递归的中序遍历
public static void midOrder1(Node Node)
{
Stack<Node> stack = new Stack<Node>();
while(Node != null || !stack.empty())
{
while(Node != null)
{
stack.push(Node);
Node = Node.getLeft();
}
if(!stack.empty())
{
Node = stack.pop();
System.out.println(Node + " ");
Node = Node.getRight();
}
}
}
//非递归的后序遍历
public static void posOrder1(Node Node)
{
Stack<Node> stack1 = new Stack<Node>();
Stack<Integer> stack2 = new Stack<>();
int i = 1;
while(Node != null || !stack1.empty())
{
while (Node != null)
{
stack1.push(Node);
stack2.push(0);
Node = Node.getLeft();
}
while(!stack1.empty() && stack2.peek() == i)
{
stack2.pop();
System.out.println(stack1.pop() + " ");
}
if(!stack1.empty())
{
stack2.pop();
stack2.push(1);
Node = stack1.peek();
Node = Node.getRight();
}
}
}
}