华为机考

1. 判断回文

public class Huiwen {

    static boolean IsHuiwen(String org){
        StringBuffer strBf = new StringBuffer(org);
        StringBuffer strBfv = strBf.reverse();
        System.out.println("Org:"+org);
        System.out.println("OrgRe:"+strBfv);
        return org.equals(strBfv.toString());  //一定要用org,不能用strBf
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        System.out.println(IsHuiwen("abcba"));
    }

}

2. 求数组中大于平均值的个数

3. 单链表逆置


public class LinkReserve {

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub

        class Node{
            String value;
            Node next;
           
            public Node(String value){
                this.value = value;
            }
           
        }
        Node n1 = new Node("a");
        Node n2 = new Node("b");
        Node n3 = new Node("c");
        Node n4 = new Node("d");
        n1.next = n2;
        n2.next = n3;
        n3.next = n4;
       
        Node head = n1;
        Node pre = null;
        Node next = head.next;
        head.next = pre;
       
        while(next != null){
            pre = head;
            head = next;
            next = next.next;
            head.next = pre;
           
        }

        Node p = head;
        while(p != null){
            System.out.println(p.value);
            p = p.next;
        }

    }

}
4. 树的遍历

5. 大数相加

 

  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStreamReader;  
  4. import java.util.StringTokenizer;  
  5.   
  6. public class BigIntegerOperation {      
  7.       
  8.       
  9.     public static void main(String[] args) throws IOException {  
  10.           
  11.         /** 
  12.         * 从控制台读取输入数据 格式为 num1空格num2 为空则exit 
  13.         */  
  14.         BufferedReader in = new BufferedReader(new InputStreamReader(System.in));  
  15.         String str = in.readLine();  
  16.         while(str!=null){  
  17.         StringTokenizer st = new StringTokenizer(str," ");  
  18.         String num1 = st.nextToken();  
  19.         String num2 = st.nextToken();  
  20.         int len1 = num1.length();  
  21.         int len2 = num2.length();  
  22.         /** 
  23.         * 将不等长的两输入字符串格式化,比如 num1 = 123 num2 = 1234 
  24.         * 经格式化后将成为num1 = 0123 num2 = 1234为方便计算 
  25.         */  
  26.         if(len1 > len2){  
  27.             for(int i=0; i< len1-len2; i++){  
  28.                 num2 = "0"+num2;  
  29.             }  
  30.         }else if(len2>len1){  
  31.             for(int i=0; i< len2-len1; i++){  
  32.                 num1 = "0"+num1;  
  33.             }  
  34.         }  
  35.           
  36.         int[] arr1 = BigIntegerOperation.str2intArr(num1);  
  37.         int[] arr2 = BigIntegerOperation.str2intArr(num2);  
  38.           
  39.         arr1 = BigIntegerOperation.reverse(arr1);  
  40.         arr2 = BigIntegerOperation.reverse(arr2);  
  41.           
  42.           
  43.         int[] result = BigIntegerOperation.add(arr1, arr2);  
  44.           
  45. //        System.out.print(num1 +" + " + num2 +" = ");  
  46.         for(int i=result.length-1; i>=0; i--){  
  47.             if(i == result.length-1 && result[i] == 0continue;  
  48.             System.out.print(result[i]);  
  49.         }  
  50.         str = in.readLine();  
  51.         }  
  52.     }  
  53.       
  54.     /** 
  55.     * 倒置数组元素 
  56.     */  
  57.     public static int [] reverse(int[] arr){  
  58.         int len = arr.length;  
  59.         for(int i = 0,j = len-1; i<j; i++,j--){  
  60.             int temp = arr[i];  
  61.             arr[i] = arr[j];  
  62.             arr[j] = temp;  
  63.         }  
  64.         return arr;  
  65.     }  
  66.       
  67.     /** 
  68.     *     将字符串转换成整型数组 
  69.     */  
  70.     public static int [] str2intArr(String str){  
  71.         int len = str.length();  
  72.         int [] arr = new int[len];  
  73.         for(int i = 0 ; i<len; i++){  
  74.             arr[i] = str.charAt(i) - '0' ;  
  75.         }  
  76.         return arr;  
  77.     }  
  78.       
  79.     /** 
  80.     * 核心方法   两个整型数组相加      
  81.     */  
  82.     public static int [] add(int a[], int b[]){  
  83.         int maxLen = a.length;  
  84.         int[] sum = new int[maxLen+1];  
  85.           
  86.         for(int i = 0; i< maxLen ; i++){  
  87.             int tempSum = a[i] + b[i];  
  88.             sum[i] += tempSum%10;  
  89.             int d = tempSum/10//进位  
  90.             sum[i+1] += d;              
  91.         }  
  92.         return sum;  
  93.     }  
  94.   
  95. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值