Midterm

5665 ワード

  • Two Sum
  • class Solution {
        public int[] twoSum(int[] nums, int target) {
            int[] result = new int[2];
            int x = 0;
            int y = 1;
            boolean found = (nums[x] + nums[y] == target);
            while (!found && x < nums.length - 1 && y < nums.length ) {
                for (int i = 0; i < nums.length - 1; i++) {
                    x = i;
                    int xval = nums[i];
                    for (int j = i; j < nums.length; j++) {
                        y = j;
                        int yval = nums[j];
    
                        found = nums[x] + nums[y] == target;
                    }
                }
            }
    
            if (found) {
                result[0] = x;
                result[1] = y;
                return result;
            } else {
                return null;
            }
        }
    }
    testcaseは通過しましたが実戦は通過しませんでした~
    答えを見て見たのですか?私はあなたを爱してshmap私はあなたを爱して私はもっとよくすることができます🤙
  • Best Time to Buy and Sell Stock
  • class Solution {
        public int maxProfit(int[] prices) {
            if (prices.length <= 1) {
                return 0;
            }
            int maxProfit = 0;
            int curProfit = 0;
            int bought = prices[0];
            for (int i = 1; i < prices.length; i++) {
                if (prices[i] < bought) {
                    bought = prices[i];
                } else {
                    curProfit = prices[i] - bought;
                    if (curProfit > maxProfit) {
                        maxProfit = curProfit; 
                    }
                }
            }
            return maxProfit;
        }
    }
    Runtime: 1 ms, faster than 97.54% of Java online submissions for Best Time to Buy and Sell Stock.
    Memory Usage: 38.5 MB, less than 81.84% of Java online submissions for Best Time to Buy and Sell Stock.
    食べ過ぎの問題で食べ過ぎた~
  • Climbing Stairs
  • class Solution {
        public int climbStairs(int n) {
            if (n <= 2) {
                return n;
            }
            
            int one = 1;
            int two = 2;
            int cur = 3; 
            for (int i = 3; i <= n; i++) {
                cur = one + two;
                one = two;
                two = cur; 
            }
            
            return cur;
        }
    }
    Runtime: 0 ms, faster than 100.00% of Java online submissions for Climbing Stairs.
    Memory Usage: 35.9 MB, less than 25.77% of Java online submissions for Climbing Stairs.
    フィボナッチを描き直して解けました~
  • Happy Number
  • class Solution {
        public boolean isHappy(int n) {
            Set<Integer> list = new HashSet<Integer>();
            
            int start = n;
            int end = squared(n);
            
            while (end != 1 && start != end) {
                start = squared(start);
                end = squared(squared(end));
            }
            
            return end == 1;
        }
        
        private int squared(int n) {
            int val = 0;
            while (n > 0) {
                int d = n % 10;
                n = n / 10;
                val = val + d * d;
            }
            return val;
        }
    }
    Runtime: 1 ms, faster than 82.65% of Java online submissions for Happy Number.
    Memory Usage: 36.3 MB, less than 30.93% of Java online submissions for Happy Number.
    トラウマ1ハッピーナンバー...
    なぜこれが正しいのかはわかりませんが、なぜか、丸いアルゴリズムを思い出して、解けました~
  • Intersection of Two Linked Lists
  • /**
     * Definition for singly-linked list.
     * public class ListNode {
     *     int val;
     *     ListNode next;
     *     ListNode(int x) {
     *         val = x;
     *         next = null;
     *     }
     * }
     */
    public class Solution {
        public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
            if (headA == null || headB == null) {
                return null;
            }
            
            ListNode a = headA;
            ListNode b = headB;
            
            while (a != b) {
                a = a == null? headB : a.next;
                b = b == null? headA : b.next;
            }
            
            return a; 
            
        }
    }
    Runtime: 1 ms, faster than 97.67% of Java online submissions for Intersection of Two Linked Lists.
    Memory Usage: 42 MB, less than 51.17% of Java online submissions for Intersection of Two Linked Lists.
    天才解答
    今の違いを解消する方法を考えると驚きました
  • Palindrome Linked List
  • class Solution {
        public boolean isPalindrome(ListNode head) {
            if (head == null) {
                return true;
            }
            ListNode cur = head;
            List<Integer> vals = new ArrayList<Integer>();
            while (cur != null) {
                vals.add(cur.val);
                cur = cur.next;
            }
            
            int size = vals.size();
            
            for (int i = 0; i < size / 2; i++) {
                if (! vals.get(i).equals(vals.get(size - i - 1))) {
                    return false;
                }
            }
            
            return true; 
        }
    }
    Runtime: 3 ms, faster than 25.21% of Java online submissions for Palindrome Linked List.
    Memory Usage: 42.9 MB, less than 33.67% of Java online submissions for Palindrome Linked List.