LeetCode解题路程(1)

不知不觉,终于迎来了找工作的日子,我知道自己是个noob,但也想写点什么,做点什么,就写点leetcode的解题,不是见解,更不是教程,只是记录

397.IntegerReplacement

Given a positive integer n and you can do operations as follow:

  1. If n is even, replace n with n/2.
  2. If n is odd, you can replace n with either n + 1 or n - 1. What is the minimum number of replacements needed for n to become 1?

Example 1:

Input:

8

Output:

3

Explanation:

8 -> 4 -> 2 -> 1

这题目是easy级的,可是看到具体题目发现不是很好下手

第一个想到的是数学解法,找规律求解,可是没找到

第二个想到的是dp,可以求出路上所有数的最小步骤,状态转移也简单

刚写了个dp数组就发现这种做法蠢爆了

最后才想到递归,一直不是很喜欢用递归,有些时候还是很好用的

最后一个问题是大数问题,他有一个测试用例是 int_max,报stackoverflow异常,这里用了非常蠢的取巧的 办法,直接return32,希望以后能有所改进

public  int integerReplacement(int n) {
        if(n==Integer.MAX_VALUE){
            return 32;
        }else{
            return step(n,0);
        }
    }

public  int step(int n,int count){
        if(n==1){
            return count;
        }else if(n%2==0){
            return step(n/2,count+1);
        }else{
            return Math.min(step(n+1,count+1),step(n-1,count+1));
        }
    }

396. Rotate Function

Given an array of integers A and let n to be its length.

Assume Bk to be an array obtained by rotating the array A k positions clock-wise, we define a "rotation function" F on A as follow:

F(k) = 0 * Bk[0] + 1 * Bk[1] + ... + (n-1) * Bk[n-1].

Calculate the maximum value of F(0), F(1), ..., F(n-1).

Note: n is guaranteed to be less than 105.

Example:

A = [4, 3, 2, 6]

F(0) = (0 * 4) + (1 * 3) + (2 * 2) + (3 * 6) = 0 + 3 + 4 + 18 = 25

F(1) = (0 * 6) + (1 * 4) + (2 * 3) + (3 * 2) = 0 + 4 + 6 + 6 = 16

F(2) = (0 * 2) + (1 * 6) + (2 * 4) + (3 * 3) = 0 + 6 + 8 + 9 = 23

F(3) = (0 * 3) + (1 * 2) + (2 * 6) + (3 * 4) = 0 + 2 + 12 + 12 = 26

So the maximum value of F(0), F(1), F(2), F(3) is F(3) = 26.

看起来非常简单的一道题,两个循环求出max即可。

可验证的时候果然想的太简单了,sum在加的过程中越界。

用long做好像也越界,开始寻找不同f之间的关系

还是挺容易找到的

f(0) =0A+1B+2C+3D

f(1) =3A+0B+1C+2D

能够发现,f(1) = f(0) - (A+B+C+D) + 4A

那么求解出f(0),其他只要一次循环就能出来了,O(n)就行

   public int maxRotateFunction(int[] A) {
        int sum=0;
        int f=0;
        for(int i=0;i<A.length;++i){
            sum+=A[i];
            f+=i*A[i];
        }
        int max=f;
        for(int i=0;i<A.length;++i){
            f = f-sum+A.length*A[i];
            max=Math.max(f,max);
        }
        return max;
    }

389. Find the Difference

Given two strings s and t which consist of only lowercase letters.

String t is generated by random shuffling string s and then add one more letter at a random position.

Find the letter that was added in t.

Example:

Input:

s = "abcd"

t = "adceb"

Output:

e

Explanation:

'e' is the letter that was added.

方法1:用map,把t的char全部放入map中,然后减去s,value大于0的那个就是多出来的

方法2:XOR,XOR能得出一组成双的数中唯一单个的数,'a'^'b'^'c'^'b'^'a' = 'c'

利用这个特性对两个字符串的全部字符进行XOR操作,得出的那个数一定是要求的char

    public char findTheDifference(String s, String t) {
        s= s+t;
        char c = s.charAt(0);
        for(int i=1;i<s.length();++i){
            c^=s.charAt(i);
        }
        return c;
    }

转载于:https://my.oschina.net/u/2486965/blog/746398

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值