删除链表的倒数第n个结点&&大数加法&&按之字形顺序打印二叉树

131 阅读1分钟

NC53 删除链表的倒数第n个结点

题目链接

1、解题思路
  • 先让快指针跑n步,随后快慢指针同步。快指针为空此时,说明慢指针到了要删除的节点了。
2、代码
import java.util.*;

/*
 * public class ListNode {
 *   int val;
 *   ListNode next = null;
 * }
 */

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @param n int整型 
     * @return ListNode类
     */
    public ListNode removeNthFromEnd (ListNode head, int n) {
        // write code here
        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode fast = dummy;
        ListNode slow = dummy;
        for(int i = 0;i < n;i++){
            fast = fast.next;
        }
        while(fast.next != null){
            fast = fast.next;
            slow = slow.next;
        }
        slow.next = slow.next.next;
        return dummy.next;
    }
}

NC1 大数加法

题目链接

1、解题思路
  • 使用双指针,注意保存进位位。把结果保存在字符串中,最后记得反转字符串。
2、代码
import java.util.*;


public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 计算两个数之和
     * @param s string字符串 表示第一个整数
     * @param t string字符串 表示第二个整数
     * @return string字符串
     */
    public String solve (String s, String t) {
        // write code here
        int len1 = s.length() - 1;
        int len2 = t.length() - 1;
        int carry = 0;
        StringBuilder sb = new StringBuilder();
        while(len1 >= 0 || len2 >= 0){
            int val1 = len1 >= 0 ? (s.charAt(len1--) - '0') : 0;
            int val2 = len2 >= 0 ? (t.charAt(len2--) - '0') : 0;
            int val = val1 + val2 + carry;
            carry = val / 10;
            val = val % 10;
            sb.append(val);
        }
        if(carry != 0){
            sb.append(carry);
        }
        return sb.reverse().toString();
    }
}

NC14 按之字形顺序打印二叉树

题目链接

1、解题思路
  • 层次遍历,然后注意有的需要逆转。
2、代码
import java.util.*;

/*
public class TreeNode {
    int val = 0;
    TreeNode left = null;
    TreeNode right = null;

    public TreeNode(int val) {
        this.val = val;

    }

}
*/
public class Solution {
    
    private void reverse(ArrayList<Integer> list){
        int l = 0;
        int r = list.size() - 1;
        while(l <= r){
            int val = list.get(l);
            list.set(l,list.get(r));
            list.set(r,val);
            l++;
            r--;
        }
    }
    
    public ArrayList<ArrayList<Integer> > Print(TreeNode pRoot) {
        ArrayList<ArrayList<Integer>> ret = new ArrayList<>();
        if(pRoot == null){
            return ret;
        }
        Queue<TreeNode> queue = new LinkedList<>();
        queue.add(pRoot);
        int index = 0;
        while(!queue.isEmpty()){
            index++;
            int size = queue.size();
            ArrayList<Integer> temp = new ArrayList<>();
            for(int i = 0;i < size;i++){
                TreeNode node = queue.poll();
                if(node.left != null){
                    queue.add(node.left);
                }
                if(node.right != null){
                    queue.add(node.right);
                }
                temp.add(node.val);
            }
            if(index % 2 == 0){
                reverse(temp);
            }
            ret.add(temp);
        }
        return ret;
    }

}