面试算法题

    /**
     * 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。
     * 但是要保证汉字不被截半个,如输入(“我ABC”,4),应该输出“我AB”;
     * 输入(“我ABC汉DEF”,6),应该输出“我ABC”而不是“我ABC”+“汉”的半个
     *
     * 考点:汉字截半时对应字节的ASCII码小于0
     */

    public static void main(String[] args) throws Exception {
        String src = "我ABC汉DEF";
        System.out.println(spiltString(src, 4));
        System.out.println(spiltString(src, 6));
    }

    private static String spiltString(String src, int len) throws Exception {
        if (src == null || src.equals("")) {
            System.out.println("The source String is null!");
            return null;
        }
        byte[] srcBytes = src.getBytes("GBK");
        if (len > srcBytes.length) {
            len = srcBytes.length;
        }
        if (srcBytes[len] < 0) {
            return new String(srcBytes, 0, --len);
        } else {
            return new String(srcBytes, 0, len);
        }
    }
  /**
     * 在实际开发工作中,对字符串的处理是最常见的编程任务。
     * 本题目即是要求程序对用户输入的字符串进行处理。具体规则如下:
     * a)把每个单词的首字母变为大写
     * b)把数字与字母之间用下划线字符(_)分开,使得更清晰
     * c)把单词中间有多个空格的调整为1个空格
     * 例如:
     * 用户输入:
     * you and me what      cpp2005program
     * 则程序输出:
     * You And Me What Cpp_2005_program
     * 
     * 相关文章:http://blog.csdn.net/u013091087/article/details/43793149
     */

    public static void main(String[] args) {
        System.out.println("please input:");
        Scanner scanner = new Scanner(System.in);
        String s = scanner.nextLine();
        scanner.close();
        String[] ss = s.split("\\\\s+"); // \\s表示空格、\\t、\\n等空白字符
        for (int i = 0; i < ss.length; i++) {
            String up = (ss[i].charAt(0) + "").toUpperCase(); // 大写
            StringBuffer sb = new StringBuffer(ss[i]);
            ss[i] = sb.replace(0, 1, up).toString(); // 首字母替换为大写
            Matcher m = Pattern.compile("\\\\d+").matcher(ss[i]);
            int fromIndex = 0;
            while (m.find()) {
                String num = m.group();
                int index = ss[i].indexOf(num, fromIndex);
                StringBuffer sbNum = new StringBuffer(ss[i]);
                ss[i] = sbNum.replace(index, index + num.length(),
                        "_" + num + "_").toString();
                fromIndex = index + num.length() + 2;
                if (ss[i].startsWith("_")) { // 去头"_"
                    ss[i] = ss[i].substring(1);
                }
                if (ss[i].endsWith("_")) { // 去尾"_"
                    ss[i] = ss[i].substring(0, ss[i].length() - 1);
                }
            }
        }
        for (int i = 0; i < ss.length - 1; i++) {
            System.out.print(ss[i] + " ");
        }
        System.out.print(ss[ss.length - 1]);
    }
    /**
     * 两个有序数组的合并函数
     * @param a
     * @param b
     * @return
     */
    public static int[] MergeList(int a[], int b[]) {
        int result[];
        result = new int[a.length + b.length];
        //i:用于标示a数组    j:用来标示b数组  k:用来标示传入的数组
        int i = 0, j = 0, k = 0;

        while (i < a.length && j < b.length) {
            if (a[i] <= b[j]) {
                result[k++] = a[i++];
            } else {
                result[k++] = b[j++];
            }
        }
            /* 后面连个while循环是用来保证两个数组比较完之后剩下的一个数组里的元素能顺利传入 */
        while (i < a.length)
            result[k++] = a[i++];
        while (j < b.length)
            result[k++] = b[j++];

        return result;
    }
    public static int rank(int key, int a[]) {
        return rank(key, a, 0, a.length - 1);
    }

    public static int rank(int key, int a[], int lo, int hi) {
        if (lo > hi)
            return -1;//error
        int mid = lo + (hi - lo) / 2;
        if (key < a[mid])
            return rank(key, a, lo, mid - 1);
        else if (key > a[mid])
            return rank(key, a, mid + 1, hi);
        else
            return mid;
    }

/**
 * 将数组中奇数放到偶数前面java实现
 * 例如:给定 int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};
 *      输出 int[] nums = {1, 9, 3, 7, 5, 6, 4, 8, 2};
 */
public class TokenCode1 {
    public static void main(String[] args) {
        // write your code here
        int[] nums = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] result = convertNums(nums);
        System.out.println(Arrays.toString(result));
    }

    private static int[] convertNums(int[] nums) {
        int i = 0;
        int j = nums.length - 1;
        while (i < j) {
            while (i < j && nums[i] % 2 != 0) {
                i++;
            }
            while (i < j && nums[j] % 2 == 0) {
                j--;
            }
            swap(nums, i, j);
        }
        return nums;
    }

    private static void swap(int[] nums, int a, int b) {
        int temp = nums[a];
        nums[a] = nums[b];
        nums[b] = temp;
    }
}
/**
 * 链表反转
 */
public class TokenCode2 {
    public static void main(String[] args) {
        Node headNode = buildTestNode();
        printNodes(headNode);
        printNodes(convertNodes(headNode));
    }

    /**
     * 链表反转
     * @param node
     * @return
     */
    private static Node convertNodes(Node node) {
        Node pre = null;
        Node now = node;
        while (now != null) {
            Node next = now.child;
            now.child = pre;
            pre = now;
            now = next;
        }
        return pre;
    }


    /**
     * 生成测试头节点
     * @return
     */
    private static Node buildTestNode() {
        Node node5 = new Node("5", null);
        Node node4 = new Node("4", node5);
        Node node3 = new Node("3", node4);
        Node node2 = new Node("2", node3);
        Node head = new Node("1", node2);
        return head;
    }

    /**
     * 打印列表
     * @param node
     */
    private static void printNodes(Node node) {
        System.out.print(node.value);
        if (node.child != null) {
            System.out.print(" -> ");
            printNodes(node.child);
        } else {
            System.out.println();
        }
    }
}
/**
 * 合并两个有序链表
 * 例如
 * headA = 1 -> 3 -> 5 -> 7 -> 9
 * headB = 2 -> 4 -> 6 -> 8 -> 10
 * result = 1 -> 2 -> 3 -> 4 -> 5 -> 6 -> 7 -> 8 -> 9 -> 10
 */
public class TokenCode3 {
    public static void main(String[] args) {
        Node headA = buildTestNode_A();
        printNodes(headA);
        Node headB = buildTestNode_B();
        printNodes(headB);
        printNodes(mergeNodes(headA, headB));
    }

    /**
     * 合并数组
     * @param headA
     * @param headB
     * @return
     */
    private static Node mergeNodes(Node headA, Node headB) {

        if (headA == null) {
            return headB;
        }
        if (headB == null) {
            return headA;
        }

        Node result;
        if (headA.value <= headB.value) {
            result = headA;
            result.next = mergeNodes(headA.next, headB);
        } else {
            result = headB;
            result.next = mergeNodes(headA, headB.next);
        }
        return result;
    }

    /**
     * 生成测试头节点
     *
     * @return
     */
    private static Node buildTestNode_A() {
        Node node5 = new Node(9, null);
        Node node4 = new Node(7, node5);
        Node node3 = new Node(5, node4);
        Node node2 = new Node(3, node3);
        Node head = new Node(1, node2);
        return head;
    }

    /**
     * 生成测试头节点
     *
     * @return
     */
    private static Node buildTestNode_B() {
        Node node5 = new Node(10, null);
        Node node4 = new Node(8, node5);
        Node node3 = new Node(6, node4);
        Node node2 = new Node(4, node3);
        Node head = new Node(2, node2);
        return head;
    }

    /**
     * 打印列表
     *
     * @param node
     */
    private static void printNodes(Node node) {
        System.out.print(node.value);
        if (node.next != null) {
            System.out.print(" -> ");
            printNodes(node.next);
        } else {
            System.out.println();
        }
    }
}
/**
 * 树的最大深度
 */
public class TokenCode4 {
    public static void main(String[] args) {
        TreeNode head = buildTestTree();
        printTree(head);
        System.out.println();
        System.out.print("count = " + getCount(head));
    }

    /**
     * 树的最大深度
     * @param treeNode
     * @return
     */
    private static int getCount(TreeNode treeNode) {
        if (treeNode == null) {
            return 0;
        } else {
            int left = getCount(treeNode.left);
            int right = getCount(treeNode.right);
            if (left > right) {
                return left + 1;
            } else {
                return right + 1;
            }
        }
    }

    /**
     * 测试树 1 , 2 , 4 , 5 , 3 , 6 , 7
     * @return
     */
    private static TreeNode buildTestTree() {
        TreeNode node7 = new TreeNode(7, null, null);
        TreeNode node6 = new TreeNode(6, null, null);
        TreeNode node5 = new TreeNode(5, null, null);
        TreeNode node4 = new TreeNode(4, null, null);
        TreeNode node3 = new TreeNode(3, node6, node7);
        TreeNode node2 = new TreeNode(2, node4, node5);
        TreeNode node1 = new TreeNode(1, node2, node3);
        return node1;
    }

    /**
     * 遍历树
     * @param treeNode
     */
    private static void printTree(TreeNode treeNode) {
        System.out.print(treeNode.value);

        if (treeNode.left != null) {
            System.out.print(" , ");
            printTree(treeNode.left);
        }

        if (treeNode.right != null) {
            System.out.print(" , ");
            printTree(treeNode.right);
        }
    }
}
/**
 * 二分法查找
 * 给定数组 nums = {3, 6, 7, 9, 15, 20, 26, 33, 56, 63}
 * 查找目标数组 33 的下标
 */
public class TokenCode5 {
    public static void main(String[] args) {
        int[] nums = {3, 6, 7, 9, 15, 20, 26, 33, 56, 63};
        int target = 33;
        System.out.print("index = " + binarysearch(nums, target));
    }


    /**
     * 二分法查找
     *
     * @param nums
     * @param target
     * @return
     */
    private static int binarysearch(int[] nums, int target) {
        int low = 0;
        int high = nums.length - 1;
        int mid = 0;
        while (low <= high) {
            mid = (low + high) / 2;

            if (nums[mid] == target) {
                return mid;
            }
            if (nums[mid] > target) {
                high = mid - 1;
            } else {
                low = mid + 1;
            }
        }
        return -1;
    }
}

你可能感兴趣的:(面试算法题)