[2016/06/23] LeetCode OJ / Java

7 篇文章 0 订阅

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">从今天开始撸LeetCode。嗯,AC率从高到低来刷。</span>


344. Reverse String

Write a function that takes a string as input and returns the string reversed.

Example:
Given s = "hello", return "olleh".

-----------------------------------------------------------------------------------------------------------------------------------------
【思路】很简单。倒过来赋值即可。除了需要考虑一下空串。。。[不要问我怎么知道的!

public class Solution {
    public static String reverseString(String s) {
    	if(s.length()==0)	return "";
    	char[] sCopy = new char[s.length()];
        for(int i=0; i< s.length()/2+1; i++){
        	sCopy[i]=s.charAt(s.length()-i-1);
        	sCopy[s.length()-i-1]=s.charAt(i);
        }
		return new String(sCopy);
    }
    public static void main(String[] args) {
		System.out.println(reverseString(""));
	}
}


292. Counting Bits

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.

Example:
For num = 5 you should return [0,1,1,2,1,2].

Follow up:

  • It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
  • Space complexity should be O(n).
  • Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

【思路】老实说。不看Hint我真的不知道O(n)的算法怎么写。。因为我是暴力无脑党(捂心口)。
我是这么思考的。(根据提示)把区间划分为[0,1] [2,3] [4,7] [8,15]...,然后你会发现,当前所需要表示成二进制的i,减去它所在区间的下限值,得到的数字(索引)在数组中本身就是有值的。比如需要表示5的时候,5-4=1,那么count[1]是等于1的,加上这个区间本身自带的1个1(最高位),就是2个1。再比如说,需要表示13的时候,13-8=5,那么我5之前计算出来了,那么就是2+1=3。
记得算到下限值的时候要更新一下当前所在的区间,和区间的上下限值。

public class Solution {
    public int[] countBits(int num) {
        int twom=0;
        int[] count = new int[num+1];
        int twolast1=0;
        int twolast2=(int)Math.pow(2, twom+1)-1;
        //当前左右区间
        count[0]=0;
        for(int i=1;i<=num;i++){
        	if(i==1)	count[1]=1;
        	else if(i>twolast1 && i<=twolast2)
        	{
        		count[i] = count[twolast1]+count[i-twolast1];
        	}else{
        		count[i]=1;
        		twom++;
        		twolast1=(int)Math.pow(2, twom);
                twolast2=(int)Math.pow(2, twom+1)-1;
        	}        		
        }
        return count;
    }    
}


292. Nim Game

You are playing the following Nim Game with your friend: There is a heap of stones on the table, each time one of you take turns to remove 1 to 3 stones. The one who removes the last stone will be the winner. You will take the first turn to remove the stones.

Both of you are very clever and have optimal strategies for the game. Write a function to determine whether you can win the game given the number of stones in the heap.

For example, if there are 4 stones in the heap, then you will never win the game: no matter 1, 2, or 3 stones you remove, the last stone will always be removed by your friend.

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------

【思路】
今天最大的收获!就是!我终于会了这种 你选一下 我选一下 看谁能赢 的题目!hhh
其实很简单,和上面一题甚至很像,只要你灵活运用你已经获取到的信息。

我们可以这么来看。石子数为1/2/3的时候,先拿的人必赢,对吧?
那么石子数为4的时候,如果先拿的人拿了1个,剩下的人就是面对3个——转化成另外一个问题,一共有3个石子,他先拿,他必赢吗?必赢。如果先拿的人拿了2个,另外一个人也必赢;同理先拿的人拿了3个,另外一个人也必赢。
所以可以建立一个数组,win[num],这个数组里存储的是,num个石子,先拿的人是不是必赢。

前3个直接初始化为true,第四个开始的时候,依次尝试拿1/2/3个,如果都失败,则不可能赢。

public class Solution {
    public static boolean canWinNim(int n) {
        boolean[] IFirst = new boolean[n+1];
        IFirst[1]=true;
        if (n==1) return IFirst[1];
        IFirst[2]=true;
        if (n==2) return IFirst[2];        
        IFirst[3]=true;
        if(n==3) return IFirst[3];        
        
        for(int i=4;i<=n;i++){
		int failnum=0;
        	for(int j=1;j<=3;j++){        		
        		//如果此时我先选择,剩下来的数目使得你有获胜的机会
        		if(IFirst[i-j]){
        			failnum++;
        		}        			
        	}
        	//如果不论怎样选择,你都会获胜
        	if(failnum==3){
        		IFirst[i] = false;
        	}else{
        		IFirst[i]=true;
        	}
        }
        
        return IFirst[n];
    }

}

这个结果是没什么问题的。然后Submit,内存超了。。。
然后我最后还是AC了。只有一行代码。。。

public class Solution {
    public boolean canWinNim(int n) {
    	return n%4==0? false:true;
    }
}

为什么?自己想。。。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值