LeetCode【HOT100<一>】

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

public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
// 在两条链表上的指针
        ListNode p1 = l1, p2 = l2;
        // 虚拟头结点(构建新链表时的常用技巧)
        ListNode dummy = new ListNode(-1);
        // 指针 p 负责构建新链表
        ListNode p = dummy;
        // 记录进位
        int carry = 0;
        // 开始执行加法,两条链表走完且没有进位时才能结束循环
        while (p1 != null || p2 != null || carry > 0) {
            // 先加上上次的进位
            int val = carry;
            if (p1 != null) {
                val += p1.val;
                p1 = p1.next;
            }
            if (p2 != null) {
                val += p2.val;
                p2 = p2.next;
            }
            // 处理进位情况
            carry = val / 10;
            val = val % 10;
            // 构建新节点
            p.next = new ListNode(val);
            p = p.next;
        }
        // 返回结果链表的头结点(去除虚拟头结点)
        return dummy.next;
    }

 public int lengthOfLongestSubstring(String s) {

        if (s.length() == 0){
            return 0;
        }
        HashMap<Character,Integer> map = new HashMap<>();
        int max  = 0;
        int left = 0;
        for (int i = 0; i < s.length(); i++){
            if (map.containsKey(s.charAt(i))){
                left = Math.max(left,map.get(s.charAt(i)) + 1);
            }
            map.put(s.charAt(i),i);
            max = Math.max(max,i - left + 1);
        }
        return max;
    }

 public String longestPalindrome(String s) {
        String res = "";
        for (int i = 0; i < s.length(); i++){
            String s1 = palindrom(s,i,i);
            String s2 = palindrom(s,i,i+1);

            res = res.length() > s1.length()? res : s1;
            res = res.length() > s2.length()? res : s2;
        }
        return res;
    }

    private String palindrom(String s, int l, int r) {
        while (l >=0 && r < s.length() && s.charAt(l) == s.charAt(r)){
            l--;
            r++;
        }
        return s.substring(l+1,r);
        
    }

public int maxArea(int[] height) {
        int left = 0,right = height.length - 1;
        int res = 0;
        while (left < right){
            int cur = Math.min(height[left],height[right]) * (right - left);
            res = Math.max(res,cur);
            if (height[left] < height[right]){
                left++;
            }else {
                right--;
            }
        }
        return res;
    }

  String[] mapping = new String[]{
            "","","abc","def","ghi","jkl","mno","pqrs","tuv","wxyz"
    };
    List<String> res = new LinkedList<>();
    public List<String> letterCombinations(String digits) {
        if (digits.isEmpty()) {
            return res;
        }
        backtrack(digits, 0, new StringBuilder());
        return res;
    }
    private void backtrack(String digits, int start, StringBuilder sb) {
        if (sb.length() == digits.length()){
            res.add(sb.toString());
            return;
        }
        for (int i = start; i < digits.length(); i++){
            int digit = digits.charAt(i) - '0';
            for (char c : mapping[digit].toCharArray()){
                sb.append(c);
                backtrack(digits,i+1,sb);
                sb.deleteCharAt(sb.length()-1);
            }
        }
    }

 public ListNode removeNthFromEnd(ListNode head, int n) {

        ListNode dummy = new ListNode(0,head);
        Stack<ListNode> stack = new Stack<>();
        ListNode cur = dummy;
        while (cur != null){
            stack.push(cur);
            cur = cur.next;
        }

        for (int i = 0; i < n; i++){
            stack.pop();
        }
        ListNode prev = stack.peek();
        prev.next = prev.next.next;
        return dummy.next;
    }

public boolean isValid(String s) {
       if (s.length() ==0 && s == null){
            return false;
        }
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i ++){
            char ch = s.charAt(i);
            if(ch == '(' || ch == '{' || ch == '['){
                stack.push(ch);
            }else {
                if (stack.isEmpty()){
                    return false;
                }
                char tmp = stack.peek();
                if (tmp == '(' && ch == ')' || tmp == '{' && ch == '}' || tmp == '[' && ch == ']'){
                    stack.pop();
                }else {
                    return false;
                }
            }
        }
        if (stack.isEmpty()){
            return true;
        }else {
            return false;
        }
   }

public ListNode mergeTwoLists(ListNode list1, ListNode list2) {

     if (list1 == null){
            return list2;
        }
        if (list2 == null){
            return list1;
        }

        if (list1.val < list2.val){
            list1.next = mergeTwoLists(list1.next,list2);
            return list1;
        }else {
            list2.next = mergeTwoLists(list1,list2.next);
            return list2;
        }
    }

 public List<String> result

Guess you like

Origin blog.csdn.net/qq_50156012/article/details/124574922