要求:
給定一個字符串,按照給定的字節數要求輸出,
如: 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();
}
}
}
}