美文网首页
非leetcode题目

非leetcode题目

作者: 夜皇雪 | 来源:发表于2016-11-24 05:15 被阅读0次

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

        int[] res=new int[2];
        String[] str1=string1.split("null,null");
        String[] str2=string2.split("null,null");
        int count=0;
        if(str1.length<str2.length) count=str1.length;
        else count=str2.length;
        for(int i=0;i<count;i++){
            char c1=str1[i].charAt(str1[i].length()-2);
            char c2=str2[i].charAt(str2[i].length()-2);//逗号
            if(c1!=c2){
                res[0]=c1-'0';
                res[1]=c2='0'
                break;
            }
        }

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<ListNode> 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<Tuple> l1=new ArrayList<>();
            List<Tuple> l2=new ArrayList<>();
            for(int i=0;i<a.length;i++){
                if(a[i]!=0) l1.add(new Tuple(a[i],i));
            }
            for(int i=0;i<b.length;i++){
                if(b[i]!=0) l2.add(new Tuple(b[i],i));
            }
            for(Tuple t1:l1){
                for(Tuple t2:l2){
                    if(t1.x==t2.x) res[t1.x]=t1.val*t2.val;
                }
            }
            return res;
        }

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

public static List<Integer> subsetsWithDup(int[] nums) {
        List<Integer> res = new ArrayList<Integer>();
        Arrays.sort(nums);
        helper(res, nums, 1, 0, 0);
        Collections.sort(res);
        return res;
    }
        
    private static void helper(List<Integer> 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题目

      本文链接:https://www.haomeiwen.com/subject/ndhbpttx.html