删除有序列表中重复的元素-II&&将字符串转化为整数&&反转数字

150 阅读1分钟

NC24 删除有序列表中重复的元素-II

题目链接

1、解题思路

题目中没有头结点,我们先创造出一个头结点。找到重复值的节点时候需要处理,指针往后移动,直至遇到不同的,然后修改前面的指针就好了。

2、代码
import java.util.*;

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

public class Solution {
    /**
     * 
     * @param head ListNode类 
     * @return ListNode类
     */
    public ListNode deleteDuplicates (ListNode head) {
        // write code here
        // write code here
        ListNode dummy = new ListNode(0);
        dummy.next = head;


        ListNode temp = dummy;
        while (temp.next != null) {
            int val = temp.next.val;
            if (temp.next.next == null) {
                break;
            } else {
                if (temp.next.next.val != val) {
                    temp = temp.next;
                } else {
                    ListNode node = temp.next;
                    while (node != null && node.val == val) {
                        node = node.next;
                    }
                    temp.next = node;
                }
            }
        }

        return dummy.next;
    }
}

NC100 将字符串转化为整数

题目链接

1、解题思路

首先去除字符串头的空格,然后处理后字符串第一个字符是不是+-,或者0-9,其他的都返回0,一直读取直到遇到不合法的。

2、代码
import java.util.*;


public class Solution {
    /**
     * 
     * @param str string字符串 
     * @return int整型
     */
    public int atoi (String str) {
        // write code here
        // write code here
        int index = -1;
        for (int i = 0; i < str.length(); i++) {
            if (str.charAt(i) != ' ') {
                index = i;
                break;
            }
        }
        if (index == -1) {
            return 0;
        }
        str = str.substring(index);
        if (str.length() == 0) {
            return 0;
        } else {
            boolean f = false;
            int startIndex = 0;
            if (str.charAt(0) == '-') {
                f = true;
                startIndex = 1;
            } else if (str.charAt(0) == '+') {
                f = false;
                startIndex = 1;
            } else if (str.charAt(0) >= '0' && str.charAt(0) <= '9') {
                startIndex = 0;

            } else {
                return 0;
            }
            int sum = 0;
            for (int i = startIndex; i < str.length(); i++) {
                if (str.charAt(i) >= '0' && str.charAt(i) <= '9') {
                    sum = sum * 10 + str.charAt(i) - 48;
                } else {
                    break;
                }
            }
            if (f) {
                return sum * -1;
            } else {
                return sum;
            }
        }
    }
}

NC57 反转数字

题目链接

1、解题思路

while循环处理一下就可以了,转化为long是为了处理越界问题。

2、代码
import java.util.*;


public class Solution {
    /**
     * 
     * @param x int整型 
     * @return int整型
     */
    public int reverse (int x) {
        // write code here
        boolean f = false;
        if (x < 0) {
            f = true;
            x *= -1;
        }
        long sum = 0;
        while (x != 0) {
            sum = sum * 10 + x % 10;
            x = x / 10;
        }
        if (f) {
            sum *= -1;
        }
        if (sum > Integer.MAX_VALUE || sum < Integer.MIN_VALUE) {
            return 0;
        } else {
            return (int) sum;
        }
    }
}