Basic algorithm record (4)

Basic algorithm record 4

String word and punctuation order extraction, forbidden to use split

For example, input: {"Oh, a good day!"}
Output: {"Oh", ",", "good", "day", "!"}
This is the question of hand-tear code in the two-face interview of Netease Youdao today , the question is not difficult, pay attention to the details, at that time I wrote a bug on the paper, and the time used was not satisfactory. In short, it should be cold, and the ability to tear the code by hand should really improve. The idea of ​​​​the topic is to traverse the characters, record the conditional interception of the string, and pay attention to the boundary problem.

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
 * 顺序提取字符串{Oh, a good day!}输出{"Oh",",","a","good","day","!"}
 * @author Shinelon
 *
 */
public class TestSplitString {
public static void main(String [] args) {
    Scanner sc = new Scanner(System.in);
    String str = sc.nextLine();
    TestSplitString test = new TestSplitString();
    String[] rs = test.dataSegement(str);
    for(int i=0;i<rs.length;i++) {
        System.out.println(rs[i]);
    }
}

public String [] dataSegement(String str) {
    char[] array = str.toCharArray();
    List<String> list = new ArrayList<>();
    int index = 0;
    for(int i=0;i<array.length;i++) {
        if(array[i]>='A'&&array[i]<='Z'||array[i]>='a'&&array[i]<='z') {

        }else if(array[i]==' ') {
            String string = str.substring(index,i);
            index = i+1;
            list.add(string);

        }else {
            String string1 = str.substring(index,i);
            String string2 = str.substring(i,i+1);
            if(i+1<array.length&&array[i+1]==' ') {
                index = i+2;
                i++;
            }
            index = i+1;
            list.add(string1);
            list.add(string2);

        }
    }
    String[] rs = new String[list.size()];
    for(int i=0;i<list.size();i++) {
        rs[i] = list.get(i);
    }

    return rs;
}
}

Jingdong written test question: find out X*Y = a certain number X odd number Y even number, Y minimum even number pay attention to use Long, I don't have it here. Because the maximum number is 2^63

First of all, odd and even numbers must be even numbers, excluding odd numbers, and secondly, the even numbers are the smallest, that is, the odd numbers are the largest. It is enough to find the largest technical factor in reverse order, but also pay attention to the long problem .

import java.util.Scanner;
/**
 * JD笔试
 * 找出X*Y=某数  X奇数Y偶数,Y最小偶数  注意用long。因为最大数2^63
 * @author Shinelon
 *
 */
public class FindNum {
public static void main(String [] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();

    long[] nums = new long[n];
    for(int i=0;i<n;i++) {
        nums[i]=sc.nextLong();
    }
    sc.close();
    FindNum find = new FindNum();
    find.findRs(nums);
}

public void findRs(long [] nums) {
    for(int j=0;j<nums.length;j++) {
        if(nums[j]%2!=0) {
            System.out.println("No");
            return ;
        }else {
            long temp = nums[j]-1;
            while(nums[j]%temp!=0&&temp>=1) {
                temp -=2;
            }   
            long left = temp;
            long right = nums[j]/temp;
            System.out.println(left+" "+right);
        }
    }
} 
}

The problem of determining the correct sequence of stacks

For example, 12345 45321 is the correct pop sequence, but 45123 is not, pass in two arrays, one
is . As long as you push the stack according to the push array, and then push the pop array to pop the stack, if there is a contradiction, it is wrong, and an auxiliary stack is needed to simulate. According to the push array, although it is pushed into the stack, after each push into the stack, determine whether the current element needs to be popped out of the stack according to the popped array. In this process, the stack is already out, and then just pop the stack. When popping the stack, it is judged whether the top element of the stack is the same as that of the popped array, and the same pointer is moved back. Finally, if it is correct, the popped array pointer must just be out of bounds

import java.util.Scanner;
import java.util.Stack;
/**
 * 判断栈的正确序列问题 12345 45321 45123...
 * @author Shinelon
 *
 */
public class Solution {


public static void main(String [] args){
    Solution solution = new Solution();
    Scanner sc = new Scanner(System.in);
    int size = sc.nextInt();
    int [] pushA = new int[size];
    int [] popA = new int[size];
    for(int i=0;i<size;i++) {
        pushA[i] = sc.nextInt();
    }
    for(int i=0;i<size;i++) {
        popA[i] = sc.nextInt();
    }
    Solution s = new Solution();
    boolean rs = s.IsPopOrder(pushA, popA);
    System.out.println(rs);
}
/**
 * 用一个辅助栈,假设序列是正确的,根据序列模拟出栈,若辅助栈空而序列遍历完,说明没矛盾
 * @param pushA
 * @param popA
 * @return
 */
public boolean IsPopOrder(int [] pushA,int [] popA) {

   Stack<Integer> temp = new Stack<>();
   int k=0;
   for(int i=0;i<pushA.length;i++) {
       temp.push(pushA[i]);
       if(temp.peek()==popA[0]) {
           temp.pop();
           k++;
       }
   }

   while(!temp.isEmpty()) {
       int t =temp.pop();
       if(t==popA[k]) {
           k++;
       }
   }

   if(k==popA.length) {
       return true;
   }else {
       return false;
   }


}

public int getIndexFromA(int [] A,int num) {
    for(int j=0;j<A.length;j++) {
        if(num==A[j]) {
            return j;
        }
    }
    return -1;
}

}

Move 0 such as 042013 to 421300

Move all non-0s, add 0 at the end, record a few 0s when traversing, and then move a few bits

import java.util.Scanner;
/**
* 移动0,保留原来顺序,比如 0 1 4 3 0 为 1 4 3 0 0
* @author Shinelon
*
*/
public class MoveZero {
 public static void main(String [] args) {
        Scanner sc = new Scanner(System.in);
        MoveZero move = new MoveZero();
        int n = sc.nextInt();
        int nums [] = new int[n];
        for(int k=0;k<n;k++) {
            nums[k] = sc.nextInt();
        }
        sc.close();

        move.moveZeroes(nums);
        for(int k=0;k<n;k++) {
            System.out.println(nums[k]);
        }
 }

 public void moveZeroes(int[] nums) {
     int count_zero = 0;
     for(int i=0;i<nums.length;i++) {
         if(nums[i]==0) {
             count_zero++;
         }else if(count_zero>0){
            nums[i-count_zero] = nums[i];
         }
     }
     for(int j=nums.length-count_zero;j<nums.length;j++) {
         nums[j] = 0;
     }
 }
 }

Merge ordered list

public class MergerTwoLinks {
static ListNode l1;
static ListNode l2;
static ListNode head;
class ListNode{
    int val;
    ListNode next;
}

public static void main(String [] args) {
    MergerTwoLinks merge = new MergerTwoLinks();
    merge.initList();

    merge.addNode(l1, 2);
    merge.addNode(l1, 4);

    merge.addNode(l2, 3);
    merge.addNode(l2, 4);

    ListNode rs = merge.mergeTwoLists(l1, l2);

    while(rs!=null) {
        System.out.print(rs.val);
        rs = rs.next;
    }   
}
/**
 * 思路,将两个链表表头小的节点作为合并后的头结点,然后指针后移,循环比较两个链表的头节点
 * 小的插到合并后的链表后面直到旧的链表遍历结束
 * @param l1
 * @param l2
 * @return
 */
public ListNode mergeTwoLists(ListNode l1, ListNode l2) {
    ListNode temp = null;
    if(l1.val<=l2.val && head==null) {
        head = l1;
        temp = head;
        l1 = l1.next;
    }else if(l1.val>l2.val && head==null) {
        head = l2;
        temp = head;
        l2 = l2.next;
    }   
    while(l1!=null||l2!=null) {

        if(l1==null) {
            temp.next = l2;
            //退出循环,不然死循环
            break;
        }else if(l2==null){
            temp.next = l1;
            break;
        }else {
            if(l1.val<=l2.val) {
                temp.next = l1;
                temp = temp.next;
                l1 = l1.next;
            }else {
                temp.next = l2;
                temp = temp.next;
                l2 = l2.next;
            }
        }


    }
    return head;
}

public void initList() {
     l1 = new ListNode();
     l1.val=1;

     l2 = new ListNode();
     l2.val=1;
}

public void addNode(ListNode l,int x) {
    while(l.next!=null) {
         l = l.next;
    }
    l.next = new ListNode();
    l.next.val = x;
}


}

The sum of three numbers is 0

Double pointer, pay attention to remove repetition

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
/**
 * 找出数组三数之和为0的元素,不重复,双指针
 * @author Shinelon
 *
 */
public class TestThreeNum {

public static void main(String [] args) {
    Scanner sc = new Scanner(System.in);
    TestThreeNum test = new TestThreeNum();
    int n = sc.nextInt();
    int [] nums = new int[n];
    for(int i=0;i<n;i++) {
        nums[i]=sc.nextInt();
    }
    sc.close();
    //先排序
    Arrays.sort(nums);
    List<List<Integer>> rs = test.threeSum(nums);
    System.out.println(rs.toString());
}

public List<List<Integer>> threeSum(int[] nums) {
    List<List<Integer>> outList = new ArrayList<>();
    //如果大于0都是正数,不可能
    if(nums[0]>0) {
        return outList;
    }
    for(int i=0;i<nums.length-2;i++) {
        //去除重复
        if(i>0&&nums[i]==nums[i-1]) {
            continue;
        }
        int j=i+1;
        int k=nums.length-1;
        while(j<k) {
            if(nums[k]+nums[j]+nums[i]>0) {
                k--;
            }else if(nums[k]+nums[j]+nums[i]<0){
                j++;
            }else {
                List<Integer> list = new ArrayList<>();
                list.add(nums[i]);
                list.add(nums[j]);
                list.add(nums[k]);
                outList.add(list);
                //继续遍历
                k--;
                j++;
                //去除重复
                while(j<k&&nums[j]==nums[j-1]) {
                    j++;
                }
                //去除重复
                while(j<k&&nums[k]==nums[k+1]) {
                    k--;
                }

            }
        }
    }
    return outList;

}
}

longest unique string

Using HashMap, the key is a character, and the value is the index of the last occurrence. If it already exists, it indicates the current index - the last index is the length of this non-repetitive string, and then its value is updated. The index at the beginning of the string is there just last time. +1, the start of a new unique string

import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;


/**
* 最长不重复子串O(n)
* HashMap,key为字符,value为索引。当map中不存在时,直接放入,否则取出上一个索引
* 当前减去上一个索引即为当前不重复的最长长度,然后从上一个重复的数的下一个索引开始新一轮比较,
* 也就是去除重复因素的影响,将开始start+1索引。若不存在,放入之后更新长度值,也就是-start+1
* @author Shinelon
*
*/
public class TestMaxChildString {

public static void main(String [] args) {
    TestMaxChildString test = new TestMaxChildString();
    Scanner sc = new Scanner(System.in);
    String s = sc.nextLine();
    sc.close();
    int rs = test.lengthOfLongestSubstring(s);
    System.out.println(rs);
}

public int lengthOfLongestSubstring(String s) {
    char [] c = s.toCharArray(); 
    Map<Character,Integer> map = new HashMap<>();     
    int max=0;//最大值
    int start=0;//寻找不重复字串长度开始的索引
    for(int i=0;i<c.length;i++) {
        if(map.containsKey(c[i])) {
            //temp>=start,说明在寻找的合法范围内
            int temp =map.get(c[i]);
            if(temp>=start) {
                //更新,两个之间的差值即为不重复长度
                max = Math.max(i-temp,max);
                //后移一位,因为前面已经重复了,构造新的不重复串并继续比较,后面可能出现更长的不重复
                start = temp+1;
                //更新当前的索引,消除前面那个重复数的影响,因为构造的是不重复的新串
                map.put(c[i], i);
            }else {//不存在,则放入并更新值
                map.put(c[i], i);
                int rs = i-start+1;
                max = Math.max(max, rs);
            }
        }else {
            map.put(c[i], i);
            int rs = i-start+1;
            max = Math.max(max, rs);
        }
    }
   return max;
}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325683013&siteId=291194637