非leetcode题目

1,给出两棵树的前序遍历,寻找第一个值不同的叶节点

        int[] res=new int[2];
        String[] str1=string1.split("null,null");
        String[] str2=string2.split("null,null");
        int count=0;
        if(str1.length

2,给一个getline()函数每次调用可以用来获取一行输入,把注释去掉以后把剩下的内容打印。注释的格式是/* */这种

public class RemoveComment {
    
    public static void main(String[] args) {
        
        String commentString = "Hello /* this is a table */ Prashant";
        
        String val = remove(commentString.toCharArray());
        System.out.println("Final String == "+val);
        
    }
    public static String remove(char str[]) {
        boolean commentStringStart = false; 
        boolean commentStringEnd = false; 
        String finalString = "";
        for(int i = 0; i < str.length ; i++) {
            if(str[i] == '/') {
                if(str[i+1] == '*') {
                    commentStringStart = true;
                }
            }
            if(str[i] == '*') {
                if(str[i+1] == '/' && commentStringStart) {
                    commentStringEnd = true;
                    i = i + 1;
                    continue;
                }
            }
            if(!commentStringStart || commentStringEnd) {
                finalString = finalString + str[i];
            }
            
        }
        return finalString;
    }
}

3,print linked list reversely
非递归:

        Stack stack=new Stack<>();
        while(head!=null){
            stack.push(head);
            head=head.next;
        }
        while(!stack.isEmpty()){
            stack.poll().val;
        }

递归:

public void print(ListNode head){
            if(head==null) return;
            print(head.next);
            System.out.println(head.val);
        }

4,Sparse Vector Product || calculate dot of two sparse vectors.

class Tuple{
        int val,x;
        Tuple(int val,int x){
            this.val=val;
            this.x=x;
        }
    }
public int[] SparseVectorProduct(int[] a,int[] b){
            int[] res=new int[a.length];
            List l1=new ArrayList<>();
            List l2=new ArrayList<>();
            for(int i=0;i

5,求数组的乘积{2,3,5}输出{2,3,5,6,10,15,30}

public static List subsetsWithDup(int[] nums) {
        List res = new ArrayList();
        Arrays.sort(nums);
        helper(res, nums, 1, 0, 0);
        Collections.sort(res);
        return res;
    }
        
    private static void helper(List res, int[] nums, int prod, int level, int start){
        if(level != 0){
            res.add(prod);
        }
        for(int i = start; i < nums.length; i++){
            helper(res, nums, prod * nums[i], level + 1, i + 1);
            }
    }

你可能感兴趣的:(非leetcode题目)