字节跳动编程算法题及解答

字节跳动编程算法题及解答_第1张图片

第二题:我觉得应该先反转单链表

package List;

public class ListAdd {
    /**
     * 两个链表相加
     */
    public static Node listAdd(Node num1, Node num2){
        if(num1 == null || num2 == null){
            return num1 == null ? num2 : num1;
        }
        Node num1_new = reverseList1(num1);
        Node num2_new = reverseList1(num2);
        int carry = 0;
        Node newHead = null;
        while(num1_new != null && num2_new != null){
            int low = (num1_new.value + num2_new.value + carry)%10;
            Node node = new Node(low);
            node.next = newHead;
            newHead = node;
            carry = (num1_new.value + num2_new.value + carry)/10;
            num1_new = num1_new.next;
            num2_new = num2_new.next;
        }
       while(num1_new != null){
           int low = (num1_new.value  + carry)%10;
           Node node = new Node(low);
           node.next = newHead;
           newHead = node;
           carry = (num1_new.value + carry)/10;
           num1_new = num1_new.next;
       }
        while(num2_new != null){
            int low = (num2_new.value  + carry)%10;
            Node node = new Node(low);
            node.next = newHead;
            newHead = node;
            carry = (num2_new.value + carry)/10;
            num2_new = num2_new.next;
        }
        return newHead;

    }
    public static Node reverseList1(Node head){
        if(head == null || head.next == null){
            return head;
        }
        Node newHead = null;
        while(head != null){
            Node tmp = head;
            head = head.next;
            tmp.next = newHead;
            newHead = tmp;
        }
        return newHead;
    }
    public static void main(String[] args){
        Node node1 = new Node(1);
        Node node2 = new Node(2);
        Node node3 = new Node(3);
        Node node4 = new Node(4);
        Node node5 = new Node(3);
        Node node6 = new Node(4);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node5.next = node6;
        Node newHead = listAdd(node1, node5);
        Node.printList(newHead);
    }
}

第三题:

package Array;

public class Rain {
    /**
     * 积水问题
     */
    public static int rain(int[] arr){
        if(arr == null || arr.length <= 2){
            return 0;
        }
        int count = 0;
        int i = 0, j = 1;
        while(j < arr.length) {
            while (j < arr.length && arr[i] <= arr[j]) {
                i++;
                j++;
            }
            if(j == arr.length){
                return count;
            }
            int left = i, pointer = j;
            int sum = 0;
            while (pointer < arr.length && arr[pointer] < arr[left]){
                sum += (arr[left] - arr[pointer]);
                pointer++;
            }
            if (pointer == arr.length) {
                i++;
                j++;
            }else{
               count += sum;
               i = pointer;
               j = pointer + 1;
            }
        }
        return count;
    }
    public static void main(String[] args){
        int[] arr = {0,1,0,2,1,0,1,3,2,1,2,1};
        System.out.print(rain(arr));
    }


}

 

你可能感兴趣的:(数据结构和算法)