编程题训练3

来源:互联网 发布:输入框获取焦点js 编辑:程序博客网 时间:2024/06/07 03:40

编程题训练3

链表求和

  • 你有两个用链表代表的整数,其中每个节点包含一个数字。数字存储按照在原来整数中相反的顺序,使得第一个数字位于链表的开头。写出一个函数将两个整数相加,用链表形式返回和。

样例:

给出两个链表 3->1->5->null 和 5->9->2->null,返回 8->0->8->null

public class Solution {    /**     * @param l1: the first list     * @param l2: the second list     * @return: the sum list of l1 and l2      */    public ListNode addLists(ListNode l1, ListNode l2) {         ListNode listNode = new ListNode(0);        ListNode r = listNode;        int carry = 10;        int carryVal = 0;        ListNode t1 = l1;        ListNode t2 = l2;        while (t1 != null || t2 != null) {            boolean t1IsNull = t1 == null;            boolean t2IsNull = t2 == null;            int val1 = t1IsNull ? 0 : t1.val;            int val2 = t2IsNull ? 0 : t2.val;            int i = (val1 + val2 + carryVal);            //是否进位            int i1 = i - carry;            boolean isCarry = i1 >= 0;            //进位下次默认+1计算            carryVal = isCarry ? 1 : 0;            //收集结果            ListNode node = new ListNode(isCarry ? i1 : i);            r.next = node;            r = node;            t1 = t1IsNull ? null : t1.next;            t2 = t2IsNull ? null : t2.next;        }        //计算结束,有进位默认前面 + 1        if (carryVal == 1) {            r.next = new ListNode(1);        }         return listNode.next;    }}

二叉树最大深度

  • 给定一个二叉树,找出其最大深度。
/** * Definition of TreeNode: * public class TreeNode { *     public int val; *     public TreeNode left, right; *     public TreeNode(int val) { *         this.val = val; *         this.left = this.right = null; *     } * } */public class Solution {    /**     * @param root: The root of binary tree.     * @return: An integer.     */    public int maxDepth(TreeNode root) {        // write your code here          if(root==null){              return 0;          }          int left=maxDepth(root.left);          int right=maxDepth(root.right);          return left> right? left+1:right+1;     }}

翻转链表

样例

给出一个链表1->2->3->null,这个翻转后的链表为3->2->1->null

图解反转列表

/** * Definition for ListNode. * public class ListNode { *     int val; *     ListNode next; *     ListNode(int val) { *         this.val = val; *         this.next = null; *     } * } */ public class Solution {    /**     * @param head: The head of linked list.     * @return: The new head of reversed linked list.     */    public ListNode reverse(ListNode head) {        // write your code here         if (head == null) {            return head;        }        ListNode pre = head;        ListNode cur = head.next;        ListNode next = null;        while(cur != null){            next = cur.next;            cur.next = pre;            pre = cur;            cur = next;        }        head.next = null;        head = pre;        return head;    }}

转换字符串到整数

  • 实现atoi这个函数,将一个字符串转换为整数。如果没有合法的整数,返回0。如果整数超出了32位整数的范围,返回INT_MAX(2147483647)如果是正整数,或者INT_MIN(-2147483648)如果是负整数。

样例

“10” =>10

“-1” => -1

“123123123123123” => 2147483647

“1.0” => 1

public class Solution {    /**     * @param str: A string     * @return An integer     */    public int atoi(String str) {        // write your code here        //去除空格        str = str.trim();        int result = 0;        boolean isPos = true;        for(int i = 0; i < str.length(); i++){            char c = str.charAt(i);            if(i == 0 && (c =='-'||c =='+')){                isPos = c=='+'?true:false;            } else if (c>='0' && c<='9'){                // 检查溢出情况,不能放在后面,因为 resulte 是int 值                if( result > ( Integer.MAX_VALUE - (c - '0') ) / 10){                    return isPos? Integer.MAX_VALUE : Integer.MIN_VALUE;                }                //进一位                result *= 10;                //将c 转换为数据再加                result += c - '0';            } else {                //有小数点的情况,直接返回结果                return isPos?result:-result;            }        }        return isPos?result:-result;    }}
0 0