使用Java Leetcode从零单排篇1-3,6

1. 两数之和

这是一道经典的题目,使用HashMap将时间复杂度优化到O(N), 第一次写出来的代码如下:

public class Solution {
    public int[] twoSum(int[] nums, int target) {
        List<Integer> list = new ArrayList<>();
        Map<Integer,Integer> hash = new HashMap<>();
        for(int i=0;i<nums.length;i++) {
            if (hash.containsKey(target - nums[i])) {
                list.add(i);
                list.add(hash.get(target - nums[i]));
                break;
            }
            hash.put(nums[i], i);
        }
        return  list.stream().mapToInt(Integer::intValue).toArray();
    }
}

这样代码是可以通过的,其中使用了Java中ArrayList和HashMap,因为答案需要返回int[], 最好Integer的ArrayList返回时遇到一些困难,不方便使用API直接转化,最好使用了JDK8新特性。

下面利用这道题目的特点,不用ArrayList,优化如下:

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

这段代码已经是本题最优解法。

 

2. 两数相加

经过debug后写出的第一个通过代码,链表使用dummy技巧

public class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int add = 0;
        ListNode dummy = new ListNode(0);
        ListNode cur = dummy;
        while(l1!=null || l2!=null){
            int temp  = (l1==null?0:l1.val) + (l2==null?0:l2.val) + add;
            if(temp>=10){
                add=1;
                temp-=10;
            }else add = 0;
            if(l1!=null) l1=l1.next;
            if(l2!=null) l2=l2.next;
            cur.next = new ListNode(temp);
            cur = cur.next;
        }
        if(add==1){
            cur.next = new ListNode(1);
        }
        return dummy.next;
    }
}

代码优雅性调整

public class ListNode {
    int val;
    ListNode next;
    ListNode(int x) { val = x; }
}

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        int add = 0;
        ListNode dummy = new ListNode(0);
        ListNode cur = dummy;
        while(l1!=null || l2!=null){
            int sum  = (l1==null?0:l1.val) + (l2==null?0:l2.val) + add;
            add = sum/10;
            cur.next = new ListNode(sum%10);
            cur = cur.next;
            if(l1!=null) l1=l1.next;
            if(l2!=null) l2=l2.next;
        }
        if(add==1){
            cur.next = new ListNode(add);
        }
        return dummy.next;
    }
}

3. 无重复字符的最长子串

这道题目是典型的双指针滑动窗口问题

第一次写出的bug代码

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        Set<Character> set = new HashSet<>();
        int res = 0;
        for(int i=0,j=0;j<s.length();j++){
            while(set.contains(s.charAt(i))){
                set.remove(s.charAt(i++));
            }
            set.add(s.charAt(j));
            //System.out.println(i);
            res = Math.max(res,j-i+1);
        }
        return res;
    }
}

这里思维上有个错误,经过模拟会发现,每次探测的是窗口右边,所以正确的代码是

public class Solution {
    public int lengthOfLongestSubstring(String s) {
        Set<Character> set = new HashSet<>();
        int res = 0;
        for(int i=0,j=0;j<s.length();j++){
            while(set.contains(s.charAt(j))){
                set.remove(s.charAt(i++));
            }
            set.add(s.charAt(j));
            System.out.println(i);
            res = Math.max(res,j-i+1);
        }
        return res;
    }
}

个人认为这段代码比较直接,达到此即可。

6. Z 字形变换

这道题目属于找规律的题目,面试一般不会考,但会对于使用Java StringBuild的练习帮助很大,使用StringBuild数组充当二维字符串数组,注意Java语言特点,需要先每一行new对象。

最好将sb变为string也比较有技巧,将后面的加到第一列即可,可以直接append sb。不需要再new一个SB

public class Solution {
    public String convert(String s, int numRows) {
        StringBuffer[] sb = new StringBuffer[numRows];
        for(int i = 0; i < sb.length; i++)
            sb[i] = new StringBuffer();
        for(int i=0;i<s.length();i++){
            int index = i%(2*numRows-2);
            if(index<numRows){
                sb[index].append(s.charAt(i));
            }else{
                sb[2*numRows-index-2].append(s.charAt(i));
            }
        }
        for(int i=1;i<sb.length;i++){
            sb[0].append(sb[i]);
        }
        return sb[0].toString();
    }
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值