Java练习 day1(LeetCode简单题15道)

一、两数之和

1、题目链接

点击跳转到题目位置

2、代码

class Solution {
    public int[] twoSum(int[] nums, int target) {
        for(int i = 0; i < nums.length; ++i){
            for(int j = i+1; j < nums.length; ++j){
                if(nums[i] + nums[j] == target){
                    return new int[]{i, j};
                }
            }
        }
    return new int[]{0, 0};
    }
}

二、回文数

1、题目链接

点击跳转到题目位置

2、代码

class Solution {
    public boolean isPalindrome(int x) {
        if(x < 0 || (x % 10 == 0 && x != 0)){
            return false;
        }
        int reverseNumber = 0;
        while(x > reverseNumber){
            reverseNumber = reverseNumber * 10 + x % 10;
            x /= 10;
        }
        return reverseNumber / 10 == x || reverseNumber == x;
    }
}

三、罗马数字转整数

1、题目链接

点击跳转到题目位置

2、代码

class Solution {
    Map<Character, Integer> symbplValues = new HashMap<Character, Integer>(){
        {
            put('I', 1);
            put('V', 5);
            put('X', 10);
            put('L', 50);
            put('C', 100);
            put('D', 500);
            put('M', 1000);
        }
    };
    public int romanToInt(String s) {
        int res = 0;
        int n = s.length();
        for(int i = 0; i < n; ++i){
            int num = symbplValues.get(s.charAt(i));
            if(i == n - 1){
                res += num;
                continue;
            }
            int num1 = symbplValues.get(s.charAt(i+1));
            if(num1 > num){
                res += (num1 - num);
                ++i; 
            } else{
                res += num;
            }
        }
    return res;
    }
}

四、最长公共前缀

1、题目链接

点击跳转到题目位置

2、代码

class Solution {
    public String longestCommonPrefix(String[] strs) {
        if(strs == null || strs.length == 0){
            return "";
        }
        String res = new String();
        int max_length = 1000000;
        for(int i = 0; i < strs.length; ++i){
            max_length = Math.min(max_length, strs[i].length());
        }
        for(int i = 0; i < max_length; ++i){
            char ch = strs[0].charAt(i);
            for(int j = 1; j < strs.length; ++j){
                if(strs[j].charAt(i) != ch){
                    return res;
                }
            }
            res += ch;
        }
    return res;
    }
}

五、有效的括号

1、题目链接

点击跳转到题目位置

2、代码

class Solution {
    public boolean isValid(String s) {
        int n = s.length();
        if((n & 1) == 1){
            return false;
        }
        Deque<Character> stack = new LinkedList<Character>();
        for(int i = 0; i < n; ++i){
            if(stack.isEmpty()){
                stack.push(s.charAt(i));
            } else{
                if((stack.peek() == '(' && s.charAt(i) == ')')
                || (stack.peek() == '[' && s.charAt(i) == ']')
                || (stack.peek() == '{' && s.charAt(i) == '}')
                ){
                   stack.pop(); 
                } else{
                    stack.push(s.charAt(i));
                }
            }
        }
    return stack.isEmpty();
    }
}

六、合并两个有序链表

1、题目链接

点击跳转到题目位置

2、代码

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode mergeTwoLists(ListNode list1, ListNode list2) {
        if(list1 == null){
            return list2;
        } else if(list2 == null){
            return list1;
        } else if(list1.val < list2.val){
            list1.next = mergeTwoLists(list1.next, list2);
            return list1;
        } else{
            list2.next = mergeTwoLists(list2.next, list1);
            return list2;
        }
    }
}

七、删除有序数组中的重复项

1、题目链接

点击跳转到题目位置

2、代码

class Solution {
    public int removeDuplicates(int[] nums) {
        int left = 0;
        int right = 0;
        int n = nums.length;
        while(right < n){
            while(right < n - 1 && nums[right] == nums[left]){
                ++right;
            }
            if(right == n - 1 && nums[left] == nums[right]){
                break;
            }
            ++left;
            nums[left] = nums[right];      
        }
    return left + 1;
    }
}

八、移除元素

1、题目链接

点击跳转到题目位置

2、代码

class Solution {
    public int removeElement(int[] nums, int val) {
        int left = 0;
        int right = 0;
        int n = nums.length;
        while(right < n){
            if(nums[right] == val){
                ++right;
                continue;
            } else{
                nums[left] = nums[right];
                ++left;
                ++right;
            }
        }
    return left;
    }
}

九、找出字符串中第一个匹配项的下标

1、题目链接

点击跳转到题目位置

2、代码

class Solution {
    public int strStr(String haystack, String needle) {
        for(int i = 0; i < haystack.length(); ++i){
            int flag = 0;
            if(i + needle.length() > haystack.length()){
                break;
            }
            for(int j = 0; j < needle.length(); ++j){
                if(haystack.charAt(i + j) != needle.charAt(j)){
                    flag = -1;
                    break;
                }
            }
            if(flag == 0){
                return i;
            }
        }
    return -1;
    }
}

十、搜索插入位置

1、题目链接

点击跳转到题目位置

2、代码

class Solution {
    public int searchInsert(int[] nums, int target) {
        int left = 0;
        int right = nums.length - 1;
        int ans = -1;
        while(left <= right){
            int mid = ((right - left) >> 1) + left;
            if(nums[mid] == target){
                return mid;
            } else if(nums[mid] < target){
                left = mid + 1; 
            } else{
                ans = mid;
                right = mid - 1;
            }
        }
        if(ans == -1){
            return nums.length;
        }
    return ans;
    }
}

十一、最后一个单词的长度

1、题目链接

点击跳转到题目位置

2、代码

class Solution {
    public int lengthOfLastWord(String s) {
        int len = 0;
        int flag = 0;
        for(int i = 0; i < s.length(); ++i){
            if(s.charAt(i) == ' '){
                flag = 1;
            } else{
                if(flag == 1){
                    len = 1;
                    flag = 0;
                } else{
                    ++len;
                }
            }
        }
    return len;
    }
}

十二、加一

1、题目链接

点击跳转到题目位置

2、代码

class Solution {
    public int[] plusOne(int[] digits) {
        int n = digits.length;
        for(int i = n - 1; i >= 0; --i){
            digits[i] = (digits[i] + 1) % 10;
            if(digits[i] != 0){
                return digits;
            }
        }
        digits = new int[digits.length + 1];
        digits[0] = 1;
    return digits;
    }
}

十三、二进制求和

1、题目链接

点击跳转到题目位置

2、代码

class Solution {
    public String addBinary(String a, String b) {
        StringBuffer ans = new StringBuffer();
        int i = a.length() - 1;
        int j = b.length() - 1;
        int carry = 0;
        while(i >= 0 && j >= 0){
            int num1 = a.charAt(i) - '0';
            int num2 = b.charAt(j) - '0';
            int num = (num1 + num2 + carry) % 2;
            carry = (num1 + num2 + carry) / 2;
            ans.append(num);
            --i;
            --j;
        }
        while(i >= 0){
            int num1 = a.charAt(i) - '0';
            int num = (num1 + carry) % 2;
            carry = (num1 + carry) / 2;
            ans.append(num);
            --i;
        }
        while(j >= 0){
            int num2 = b.charAt(j) - '0';
            int num = (num2 + carry) % 2;
            carry = (num2 + carry) / 2;
            ans.append(num);
            --j;
        }
        if(carry == 1){
            ans.append(1);
        }
        ans.reverse();
    return ans.toString();
    }
}

十四、x 的平方根

1、题目链接

点击跳转到题目位置

2、代码

class Solution {
    public int mySqrt(int x) {
        int left = 0;
        int right = x;
        int ans = -1;
        while(left <= right){
            int mid = ((right - left) >> 1) + left;
            long num = (long)mid * mid;
            if(num == x){
                return mid;
            } else if(num < x){
                ans = mid;
                left = mid + 1;
            } else{
                right = mid - 1;
            }
        }
    return ans;
    }
}

十五 爬楼梯

1、题目链接

点击跳转到题目位置

2、代码

class Solution {
    public int climbStairs(int n) {
        int []dp = new int[n+1];
        dp[0] = 1;
        dp[1] = 1;
        for(int i = 2; i <= n; ++i){
            dp[i] = dp[i-1] + dp[i-2];
        }
    return dp[n];
    }
}

你可能感兴趣的:(java,leetcode)