LeetCode题解-java

1. Two Sum

Given an array of integers, return indices of the two numbers such that they add up to a specific target.

You may assume that each input would have exactly one solution, and you may not use the same element twice.

Example:
Given nums = [2, 7, 11, 15], target = 9,

Because nums[0] + nums[1] = 2 + 7 = 9,
return [0, 1].

解法1

最简单的思路。遍历数组,对每个元素,在其后面逐个查找是否有符合条件的值,若有,则返回。
最坏情况下的时间复杂度:O(N^2)
java code

public 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[2];
    }
}

解法2

利用hashtable。遍历数组,检查目标值和当前元素的差值是否在hashtable中,如果在,则返回结果。hashtable中的key为元素的值,value为元素的index.

public class Solution {
    public int[] twoSum(int[] nums, int target) {
       HashMap<Integer, Integer> aMap = new HashMap<>();
       int[] ans = new int[2];
       for (int i = 0; i < nums.length; i++) {
           if (aMap.containsKey(target - nums[i])) {
               ans[0] = aMap.get(target-nums[i]);
               ans[1] = i;
               break;
           }
           else {
               aMap.put(nums[i], i);
           }
       }
       return ans;
    }
}

2. Add Two Numbers

You are given two non-empty linked lists representing two non-negative integers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.

You may assume the two numbers do not contain any leading zero, except the number 0 itself.

Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

解法1

自己的思路。就是做简单的加法。有两个地方需要注意:1,最高位如果产生进位,则链表需要新建一个元素来保存最高位;2,记得每个数位的加法算玩后需要更新进位。

/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode(int x) { val = x; }
 * }
 */
public class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummy = new ListNode(0);
        ListNode ans = dummy;
        int carry = 0;
        while(l1 != null && l2 != null) {
            int temp = l1.val + l2.val + carry;
            if (temp >= 10) {
                carry = 1;
                ans.next = new ListNode(temp-10);
                ans = ans.next;
            }
            else {
                carry = 0;
                ans.next = new ListNode(temp);
                ans = ans.next;
            }
            l1 = l1.next;
            l2 = l2.next;
        }
        while(l1 != null) {
            int temp = l1.val + carry;
            if (temp >= 10) {
                carry = 1;
                ans.next = new ListNode(temp-10);
                ans = ans.next;
            }
            else {
                carry = 0;
                ans.next = new ListNode(temp);
                ans = ans.next;
            }
            l1 = l1.next;
        }
        while(l2 != null) {
            int temp = l2.val + carry;
            if (temp >= 10) {
                carry = 1;
                ans.next = new ListNode(temp-10);
                ans = ans.next;
            }
            else {
                carry = 0;
                ans.next = new ListNode(temp);
                ans = ans.next;
            }
            l2 = l2.next;
        }

        if (carry == 1) {
            ans.next = new ListNode(1);
            ans = ans.next;
        }
        return dummy.next;
    }
}

解法2(from Program Creek)

主题思路和自己的一样,但是利用carry位进行缓存数据,避免了重复代码,非常值得学习。
“`java
public class Solution {
public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
int carry =0;

    ListNode newHead = new ListNode(0);
    ListNode p1 = l1, p2 = l2, p3=newHead;

    while(p1 != null || p2 != null){
        if(p1 != null){
            carry += p1.val;
            p1 = p1.next;
        }

        if(p2 != null){
            carry += p2.val;
            p2 = p2.next;
        }

        p3.next = new ListNode(carry%10);
        p3 = p3.next;
        carry /= 10;
    }

    if(carry==1) 
        p3.next=new ListNode(1);

    return newHead.next;
}

}

3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.

Examples:

Given “abcabcbb”, the answer is “abc”, which the length is 3.

Given “bbbbb”, the answer is “b”, with the length of 1.

Given “pwwkew”, the answer is “wke”, with the length of 3. Note that the answer must be a substring, “pwke” is a subsequence and not a substring.

java code:

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        int map[] = new int[256];
        int i = 0;
        int j = 0;
        int ans = 0;
        for(i = 0; i < s.length(); i++) {
            while(j < s.length() && map[s.charAt(j)] == 0) {
                map[s.charAt(j)] = 1;
                ans = Math.max(ans, j-i+1);
                j++;
            }
            map[s.charAt(i)] = 0;
        }
        return ans;
    }
 }
```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值