算法打卡,用于自律

题目一

算法打卡,用于自律_第1张图片解法

class Solution {
    public int[] intersection(int[] nums1, int[] nums2) {
        int[] a = new int[1000];
        HashSet set = new HashSet();
        for(int i = 0;i iterator = set.iterator();
        while (iterator.hasNext()){
            w[p] = iterator.next();
            p++;
        }
        return w;        
    }
}

 题目二

算法打卡,用于自律_第2张图片

解法

class Solution {
    public int[] intersect(int[] nums1, int[] nums2) {
        int[] a = new int[1001];
        ArrayList list = new ArrayList();
        for(int i = 0;i0){
                list.add(nums2[i]);
                a[nums2[i]]--;
            }
        }
        int[] w = new int[list.size()];
        for(int j = 0;j

题目三

算法打卡,用于自律_第3张图片

 解法

/** 
 * Forward declaration of guess API.
 * @param  num   your guess
 * @return 	     -1 if num is lower than the guess number
 *			      1 if num is higher than the guess number
 *               otherwise return 0
 * int guess(int num);
 */

public class Solution extends GuessGame {
    public int guessNumber(int n) {
        int left = 1;
        int right = n;
        while(left<=right){
            int mid = left+(right-left)/2;
            if(guess(mid)==-1){
                right = mid-1;
            }
            if(guess(mid)==1){
                left = mid+1;
            }
            if(guess(mid)==0){
                return mid;
            }
        } 
        return 0;
    }
}

你可能感兴趣的:(算法,算法)