[2016/06/29] LeetCode / Java - Day 07 -

来源:互联网 发布:双层玻璃窗户知乎 编辑:程序博客网 时间:2024/05/14 08:10

217. Contains Duplicate

Given an array of integers, find if the array contains any duplicates. Your function should return true if any value appears at least twice in the array, and it should return false if every element is distinct.

思路:一开始暴力解之,超时。然后我才知道Java原来有一个类叫集合。。。java.util.Set...好吧毕竟不是科班出生,我好像只学到String类就停了,然后常用的也只有List和Map,居然不知道有个Set类……

import java.util.HashSet;import java.util.Set;public class Solution {    public boolean containsDuplicate(int[] nums) {        if(nums.length==0|| nums.length==1)    return false;        Set<Integer> s = new HashSet<Integer>();        for(int i=0;i<nums.length;i++){        s.add(nums[i]);        }        return s.size()==nums.length? false:true;    }}

343. Integer Break

Given a positive integer n, break it into the sum of at least two positive integers and maximize the product of those integers. Return the maximum product you can get.

For example, given n = 2, return 1 (2 = 1 + 1); given n = 10, return 36 (10 = 3 + 3 + 4).

Note: you may assume that n is not less than 2.

Hint:

  1. There is a simple O(n) solution to this problem.
  2. You may check the breaking results of n ranging from 7 to 10 to discover the regularities.
思路:看了一下提示~然后就老老实实分解从2~10,然后就发现规律啦

2=1+1,1*1=1

3=1+2,1*2=2

4=2+2,2*2=4


5=3+2,3*2=6

6=3+3,3*3=9

7=3+4,3*4=12


8=3+3+2,3*3*2=18

9=3+3+3,3*3*3=27

10=3+3+4,3*3*4=48

发现了没,大概是从5开始的规律。首先是因数中3的个数,应该是(n-2)/3个。然后是3的个数之外的别的因数,根据n除以3的余数决定,n%3=1是4,n%3=2是2,n%3=0是3。代码懒得想7之前的了,所以就直接输出了。。。

有个插曲,因为大概50~60(不太记得了)的地方,算出来的数值就超过int表示范围了。。。所以我测试了一个101,一直和答案不一样,然后我就很纳闷,一直不敢提交。。后来测试67的时候,终于出现负号了,然后我才知道过界了= =

public class Solution {    public int integerBreak(int n) {                if(n==2) return 1;        if(n==3) return 2;        if(n==4) return 4;        if(n==5) return 6;        if(n==6) return 9;        if(n==7) return 12;        int x = 9;        for(int i=0; i<(n-8)/3; i++ ){        x = x*3;        }        if(n%3==2) x=x*2;        if(n%3==0) x=x*3;        if(n%3==1) x=x*4;        return x;    }}

350. Intersection of Two Arrays II

Given two arrays, write a function to compute their intersection.

Example:
Given nums1 = [1, 2, 2, 1]nums2 = [2, 2], return [2, 2].

Note:

  • Each element in the result should appear as many times as it shows in both arrays.
  • The result can be in any order.

思路:还记得那个我只beat了0.5%的代码吗哈哈哈,这个是和那个题目一样的,但是重复的元素也要输出,也就是不能用set做了呗。我用一模一样的方法(除了改了一行之外)这次beat了75%!(好想吐槽自己不动脑子还得意。。)

public class Solution {    public int[] intersect(int[] nums1, int[] nums2) {        List<Integer> l = new ArrayList<Integer>();    Arrays.sort(nums1);Arrays.sort(nums2);    int i=0,j=0;    while(i<nums1.length && j<nums2.length)    {    int temp = nums1[i];        if(temp==nums2[j])    {    l.add(temp);    j++;i++;    }    else if (temp>nums2[j])    j++;    else if(temp<nums2[j])    i++;        }    int[] num =new  int[l.size()];    for(i=0;i<l.size();i++)    num[i] = l.get(i);return num;       }}


0 0
原创粉丝点击