LeetCode Brush title series - strings, arrays, several

[Title] 459

Given a non-empty string, it can be determined whether a string consisting of its child configuration repeated multiple times. Given string contains only lowercase letters, and the length of not more than 10000.
Here Insert Picture Description

[Thinking]

The whole idea: to turn around the current string of letters plus the current string to remove the last letter, and whether a relatively new large string that contains the original strings

[Code]

public class IsRepetedSubString {

    //整体思路:当前字符串去掉头字母加上当前字符串去掉尾字母,然后比较新的大字符串是否包含原来的那个字符串
    public static boolean repeatedSubstringPattern(String s){
        StringBuffer stringBuffer = new StringBuffer(s.substring(1,s.length()));
        stringBuffer.append(s.substring(0,s.length() - 1));
        if(stringBuffer.toString().contains(s)){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        String s = "abcabc";
        System.out.println(repeatedSubstringPattern(s));
    }

}

【result】

Here Insert Picture Description

2. The two numbers together

We are given two non-empty list is used to represent two non-negative integer. Where their respective bits are stored in reverse order of the way, and they each node can store only one digit.

If we add up these two numbers, it will return a new list and to represent them.

You can assume that in addition to the numbers 0, these two numbers will not begin with 0.

Here Insert Picture Description

package practice_FactorialAndDp;

import java.util.List;
import java.util.Stack;

/**
 * @Author: Next
 * @Date: 2020/2/10
 **/
public class LeetCode_02_addTwoNumber {

    public static class ListNode{
        public int val;
        public ListNode next;
        public ListNode(int data){
            this.val = data;
        }
    }

    public static ListNode addTwoNumber(ListNode node1, ListNode node2){
        Stack<Integer> stack = new Stack<>();
        while (node1 != null){
            stack.push(node1.val);
            node1 = node1.next;
        }
        StringBuffer sb = new StringBuffer();
        while (!stack.isEmpty()){
            sb.append(stack.pop());
        }
        int num1 = 0;
        if(sb.length() <= 9){
            num1 = Integer.parseInt(sb.toString().trim());
        }
        sb.delete(0,sb.length());
        while (node2 != null){
            stack.push(node2.val);
            node2 = node2.next;
        }
        while (!stack.isEmpty()){
            sb.append(stack.pop());
        }
        int num2 = 0;
        if(sb.length() <= 9){
            num2 = Integer.parseInt(sb.toString().trim());
        }
        sb.delete(0,sb.length());
        int resNum = num1 + num2;
        sb = new StringBuffer(resNum + "");
        String resStr = sb.reverse().toString();
        char[] chs = resStr.toCharArray();
        int[] resNums = new int[chs.length];
        for(int i = 0;i < chs.length;i++){
            if(Integer.parseInt(String.valueOf(chs[i])) < Integer.MAX_VALUE){
                resNums[i] = Integer.parseInt(String.valueOf(chs[i]));
            }
        }
        ListNode res = new ListNode(resNums[0]);//将数组中的数据转移到链表中去
        ListNode help = res;//生成另一个节点,并让help指向res节点,help在此作为一个临时变量,help和res指向同一地址
        for(int i = 1;i < resNums.length;i++){//由于已给res赋值,所以i从1开始
            ListNode temp = new ListNode(resNums[i]);//每循环一次生成一个新的节点,并给当前节点赋值
            help.next = temp;//将help的下一个节点指向生成的新的节点
            help = temp;//将help指向最后一个节点(help的下一个节点)
        }
        printNode(help);
        System.out.println("==============================");
        return res;
    }

//    public static ListNode constructNode(int[] resNums,int i){
//        if(i == resNums.length){
//
//        }
//        ListNode res = new ListNode(resNums[])
//    }


    public static ListNode addTwoNumbers(ListNode l1,ListNode l2){
        ListNode resHead = new ListNode(0);
        ListNode help1 = l1;
        ListNode help2 = l2;
        ListNode cur = resHead;
        int carry = 0;
        while (help1 != null || help2 != null){
            int h1 = (help1 != null) ? help1.val : 0;
            int h2 = (help2 != null) ? help2.val : 0;
            int sum = h1 + h2 + carry;
            carry = sum / 10;
            cur.next = new ListNode(sum % 10);
            cur = cur.next;
            if(help1 != null){
                help1 = help1.next;
            }
            if(help2 != null){
                help2 = help2.next;
            }
        }
        if(carry > 0){
            cur.next = new ListNode(carry);
        }
        return resHead.next;
    }


    public static void printNode(ListNode node){
        while (node != null){
            System.out.print(node.val + " ");
            node = node.next;
        }
        System.out.println();
    }



    public static void main(String[] args) {
        ListNode node1 = new ListNode(2);
        node1.next = new ListNode(4);
        node1.next.next = new ListNode(3);
//        ListNode node1 = new ListNode(9);
        printNode(node1);
        System.out.println("===========================");
        ListNode node2 = new ListNode(5);
        node2.next = new ListNode(6);
        node2.next.next = new ListNode(4);
//        ListNode node2 = new ListNode(1);
//        node2.next = new ListNode(9);
//        node2.next.next = new ListNode(9);
//        node2.next.next.next = new ListNode(9);
//        node2.next.next.next.next = new ListNode(9);
//        node2.next.next.next.next.next = new ListNode(9);
//        node2.next.next.next.next.next.next = new ListNode(9);
//        node2.next.next.next.next.next.next.next = new ListNode(9);
//        node2.next.next.next.next.next.next.next.next = new ListNode(9);
//        node2.next.next.next.next.next.next.next.next.next = new ListNode(9);
        printNode(node2);
        System.out.println("===========================");
//        ListNode res = addTwoNumber(node1,node2);
        ListNode res = addTwoNumbers(node1,node2);
        printNode(res);
    }

}

Here Insert Picture Description

3. The two numbers

Given an integer array nums and a target value target, and ask you to identify the target value of the two integers in the array, and return to their array subscript.

You can assume that each input corresponds to only one answer. However, you can not re-use the same array element.

Here Insert Picture Description

package practice_FactorialAndDp;

import java.util.Arrays;
import java.util.HashMap;

/**
 * @Author: Next
 * @Date: 2020/2/10
 **/
public class LeetCode_01_TwoSum {

    public static int[] twoSum(int[] nums,int target){
        if(nums == null || nums.length < 1){
            return null;
        }
        for(int i = 0;i < nums.length;i++){
            for(int j = i + 1;j < nums.length;j++){
                if(nums[j] == target - nums[i]){
                    return new int[]{i,j};
                }
            }
        }
        throw new IllegalArgumentException("No two sum solution");
    }

    public static int[] twoSum2(int[] nums,int target){
        if(nums == null || nums.length < 1){
            return null;
        }
        int i = 0;
        HashMap<Integer,Integer> map_numI = new HashMap<>();
        while(i < nums.length){
            map_numI.put(nums[i],i++);
        }
        for(i = 0;i < nums.length;i++){
            if (map_numI.containsKey(target - nums[i]) && (map_numI.get(target - nums[i]) != i)){
                return new int[]{i, map_numI.get(target - nums[i])};
            }
        }
        return null;
    }

    public static int[] twoSum3(int[] nums,int target){
        if(nums == null || nums.length < 1){
            return null;
        }
        HashMap<Integer,Integer> map = new HashMap<>();
        for(int i = 0;i < nums.length;i++){
            int complement = target - nums[i];
            if(map.containsKey(complement)){
                return new int[]{map.get(complement),i};
            }
            map.put(nums[i],i);
        }
        return null;
    }

    public static void main(String[] args) {
        int[] nums = {3,2,4};
        int target = 6;
        System.out.println(Arrays.toString(twoSum(nums,target)));
        System.out.println(Arrays.toString(twoSum2(nums,target)));
        System.out.println(Arrays.toString(twoSum3(nums,target)));
    }

}

Here Insert Picture Description

Published 122 original articles · won praise 1 · views 7339

Guess you like

Origin blog.csdn.net/qq_36079912/article/details/104232291
Recommended