非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<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);
            }
    }
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容