leetcode面試算法題

數組

  • 旋轉數組(向右移k位, 頭條)

    思路:
    
        1 2 3 4 5 6 7  如果k = 3 的話, 會變成 5 6 7 1 2 3 4
      1 2 3 4 5 6 7  middle = 7 - 3 = 4,分爲左邊 4個數字,右邊 3個數字
      4 3 2 1 7 6 5  分別把左右reverse 一下
      5 6 7 1 2 3 4  把總數組reverse 一下就會得到答案
      
    class Solution {
    public void rotate(int[] nums, int k) {
    
        int r = k %nums.length;
        int m = nums.length - r;
        change(nums,0,m-1);
        change(nums,m,nums.length-1);
        change(nums,0,nums.length-1);
    }
    
    void change(int[] nums, int l , int r){
        while(l<r){
            int temp = nums[l];
            nums[l]=nums[r];
            nums[r]=temp;
            l++;
            r--;
        }
    }
    }}
  • 最長連續遞增序列

    class Solution {
    public int findLengthOfLCIS(int[] nums) {
    
        int max=0;
    
        for(int i=0;i<nums.length;i++){
            int len=1;
            if(i<nums.length-1){
              int j=i+1;
                int k =i;
                while(j<nums.length&&nums[k]<nums[j]){
                    len++;
                    j++;
                    k++;
                }  
            }  
            max = Math.max(max, len);
        }
        return max;
    }
    }

字符串

  • 字符串中的第一個唯一字

    class Solution {
        public int firstUniqChar(String s) {
    
            //解法1
            // int[] c=new int[30];
            // for(int i =0;i<s.length();i++){
            //     c[s.charAt(i)-'a']++;
            // }
            // int index=-1;
            // for(int i=0;i<s.length();i++){
            //     if(c[s.charAt(i)-'a']==1){
            //         index=i;
            //         break;
            //     }
            // }
            // return index;
    
            //解法2
            Map<Character,Integer> map=new LinkedHashMap<>();
            for(int i=0 ;i<s.length();i++){
                map.put(s.charAt(i),map.containsKey(s.charAt(i))?map.get(s.charAt(i))+1:1);
            }
            int index=-1;
            for(Map.Entry<Character,Integer> e: map.entrySet()){
                if(e.getValue()==1){
                    index = s.indexOf(e.getKey());
                    break;
                }
            }
            return index;
        }
    }
  • 無重複最長子串

    class Solution {
        public int lengthOfLongestSubstring(String s) {
           HashMap<Character,Integer> map=new HashMap<>();
            int len=0,start=0,max=0;
            for(int i=0;i<s.length();i++){
                if(map.containsKey(s.charAt(i))&&map.get(s.charAt(i))>=start){
                    start=map.get(s.charAt(i))+1;
                }
                len = i-start+1;
                map.put(s.charAt(i),i);
                max = Math.max(len,max);
            }
            return max;
        }
        }
  • 大數相乘(騰訊二面)

    class Solution {
        public String multiply(String num1, String num2) {
            if(num1.equals("0")||num2.equals("0")){
                return "0";
            }
    
            char[] nums1=num1.toCharArray();
            char[] nums2=num2.toCharArray();
    
            int[] result = new int[nums1.length+nums2.length];
    
    
            for(int i=0;i<nums1.length;i++){
                for(int j=0;j<nums2.length;j++){
                    result[i+j] += (nums1[i]-'0')*(nums2[j]-'0');
                }
            }
    
            for(int i=result.length-1;i>0;i--){
                result[i-1] += result[i]/10;
                result[i] = result[i]% 10;
            }
    
            StringBuffer sb=new StringBuffer();
            for(int i=0;i<result.length-1;i++){
                sb.append(result[i]);
            }
            return sb.toString();
    
        }
    }}

  • 層次遍歷(頭條一面)

    public static void levelTravel(Node root){  
        if(root==)return;  
        Queue<Node> q=new LinkedList<Node>();  
        q.add(root);  
        while(!q.isEmpty()){  
            Node temp =  q.poll();  
            System.out.println(temp.value);  
            if(temp.left!=)q.add(temp.left);  
            if(temp.right!=)q.add(temp.right);  
        }  
    }  
    
     /*
     * 層序遍歷
     * 非遞歸
     */
    public void levelOrder1(BinaryNode<AnyType> Node) {
        if (Node == null) {
            return;
        }
    
        BinaryNode<AnyType> binaryNode;
        Queue<BinaryNode> queue = new LinkedList<>();
        queue.add(Node);
    
        while (queue.size() != 0) {
            binaryNode = queue.poll();
    
            System.out.print(binaryNode.element + "  ");
    
            if (binaryNode.left != null) {
                queue.offer(binaryNode.left);
            }
            if (binaryNode.right != null) {
                queue.offer(binaryNode.right);
            }
        }
    }
  • 二叉樹的遍歷 (非遞歸)

    /**
     * 前序遍歷
     * 非遞歸
     */
    public void preOrder1(BinaryNode<AnyType> Node)
    {
        Stack<BinaryNode> stack = new Stack<>();
        while(Node != null || !stack.empty())
        {
            while(Node != null)
            {
                System.out.print(Node.element + "   ");
                stack.push(Node);
                Node = Node.left;
            }
            if(!stack.empty())
            {
                Node = stack.pop();
                Node = Node.right;
            }
        }
    }
    
    /**
     * 中序遍歷
     * 非遞歸
     */
    public void midOrder1(BinaryNode<AnyType> Node)
    {
        Stack<BinaryNode> stack = new Stack<>();
        while(Node != null || !stack.empty())
        {
            while (Node != null)
            {
                stack.push(Node);
                Node = Node.left;
            }
            if(!stack.empty())
            {
                Node = stack.pop();
                System.out.print(Node.element + "   ");
                Node = Node.right;
            }
        }
    }
    
    /**
     * 後序遍歷
     * 非遞歸
     */
     public void thePostOrderTraversal_Stack(Node root) {   //後序遍歷  
        Stack<Node> stack = new Stack<Node>();  
        Stack<Node> output = new Stack<Node>();//構造一箇中間棧來存儲逆後序遍歷的結果  
        Node node = root;  
        while (node != null || stack.size() > 0) {  
            if (node != null) {  
                output.push(node);  
                stack.push(node);                 
                node = node.getRightNode();  
            } else {  
                node = stack.pop();               
                node = node.getLeftNode();
            }  
        }  
        System.out.println(output.size());
        while (output.size() > 0) {
            printNode(output.pop());    //最後的輸出
        }  
    }
    
    
    /*
     * 層序遍歷
     * 非遞歸
     */
    public void levelOrder1(BinaryNode<AnyType> Node) {
        if (Node == null) {
            return;
        }
    
        BinaryNode<AnyType> binaryNode;
        Queue<BinaryNode> queue = new LinkedList<>();
        queue.add(Node);
    
        while (queue.size() != 0) {
            binaryNode = queue.poll();
    
            System.out.print(binaryNode.element + "  ");
    
            if (binaryNode.left != null) {
                queue.offer(binaryNode.left);
            }
            if (binaryNode.right != null) {
                queue.offer(binaryNode.right);
            }
        }
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章