您的位置:首页 > 编程语言 > Java开发

LeetCode小试

2016-05-26 22:31 375 查看
最近想找实习,就找些题目练练手,感觉LeetCode不错,和大家分享一下。

334【Reverse String】

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

Example:

Given s = “hello”, return “olleh”.

Solution:

public class Solution {
public String reverseString(String s) {
int length = s.length();
int i=0;
int j=0;
char[] array = s.toCharArray();
char[] arrayTemp = new char[length];
for(i=s.length()-1, j=0; i>=0 && j<s.length(); i--,j++)
arrayTemp[j]= array[i];
return (new String(arrayTemp));
}
}


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.

思路:题目看似很复杂,其实只要发现当你取到的数字不是4的倍数时,你总可以通过抽取石子使对手下一轮手中的石子数为4的倍数,最终,当对手手中的石子数为4时,你就已经赢了。

public class Solution {
public boolean canWinNim(int n) {

if (n%4==0)
return false;
else
return true;

}
}


258 【Add Digits】

Given a non-negative integer num, repeatedly add all its digits until the result has only one digit.

For example:

Given num = 38, the process is like: 3 + 8 = 11, 1 + 1 = 2. Since 2 has only one digit, return it.

Follow up:

Could you do it without any loop/recursion in O(1) runtime?

思路:因为题目有说不用循环,那么肯定不是用常规的方法解答了。仔细想想,发现结果就是输入数对9取模。真是棒棒哒^_^。

public class Solution {
public int addDigits(int num) {
if(num == 0)
return 0;
else if(num % 9 == 0 )
return 9;
else
return num % 9;
}
}


283【Move Zeros】

Given an array nums, write a function to move all 0’s to the end of it while maintaining the relative order of the non-zero elements.

For example, given nums = [0, 1, 0, 3, 12], after calling your function, nums should be [1, 3, 12, 0, 0].

Note:

You must do this in-place without making a copy of the array.
Minimize the total number of operations.


思路:

(1)题目要求不能新建数组,而且还要使操作步骤最少。因此常规的排序比较之类的方法行不通。

(2)我的想法是:尽可能多的判断出连续的0,一步到位的交换连续0中的第一个和第一个非0的数,此时步数最少。

算法如下:

(a).首先判断是否进行到最后一个元素的判断,如果是,stop标识置为1,用来退出while循环。

(b). 接着从数组第一个元素开始判断是否为0?

如果为0,zeroSumCount++(表示0的个数),直到碰到第一个非0的数,转到步骤(c)。

(c). 如果非0,并且在此之前没有任何0,就用zeroSumCount ==0这个条件退出当前for循环,并且将location++,表示下一个循环开始的起始位置是当前非0元素的下一个元素。如果非0,并且在此之前还有一个或者多个0元素,就交换连续0中第一个0和这个第一个非0元素。同时,设置新的下一个循环的起始位置,并将zeroSumCount置0,用来重新统计0的个数。

(d)如果stop标志位有效,退出while循环,完成程序。

public class Solution {
public void moveZeroes(int[] nums) {
int location = 0;
int zeroSumCount =0;
int temp=0;
int stop =0;
while(true){
for (int i = location; i<nums.length; i++)
{
if(i==nums.length-1)
stop=1;
if(nums[i]==0)
{
//location++;
zeroSumCount++;
continue;
}
else
{
if(zeroSumCount==0)
location = i+1;
else
{
nums[i-zeroSumCount] = nums[i];
nums[i] = 0;
location = i-zeroSumCount+1;
zeroSumCount = 0;
}
break;
}
}
if(stop==1)
break;
}

}
}


希望自己明天会更好。

Best Wishes to myself !
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  LeetCode Java 算法