转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6410409.html

6:Reverse String

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

此题:字符串反转题。

思路:此题大一学C++的时候上机题就做过了,当时思路是:把String转化为char[],从尾到头遍历一次倒序地把字符复制到另一个数组(从头到尾),然后把新数组toString()即可。现在用Java做的话,把新数组换成用StringBuffer代替,倒序遍历char[]逐个append到buffer,然后toString()即可。由于每个元素都遍历了一次,复杂度O(n)。

 public String reverseString(String s) {
char[] chars=s.toCharArray();
StringBuffer buffer=new StringBuffer();
for(int i=chars.length-1;i>=0;--i){
buffer.append(chars[i]);
}
return buffer.toString();
}

第二种思路是:把char[]的前后半交换即可,只需遍历一半,复杂度O(n/2)。

 public String reverseString(String s) {
char[] chars=s.toCharArray();
int half=chars.length/2;
for(int i=0;i<half;++i){
char temp=chars[chars.length-1-i];
chars[chars.length-1-i]=chars[i];
chars[i]=temp;
}
//这里注意:char[]转化为String是通过new String(char[])实现的
return new String(chars);
}

7:Island Perimeter

You are given a map in form of a two-dimensional integer grid where 1 represents land and 0 represents water. Grid cells are connected horizontally/vertically (not diagonally). The grid is completely surrounded by water, and there is exactly one island (i.e., one or more connected land cells). The island doesn't have "lakes" (water inside that isn't connected to the water around the island). One cell is a square with side length 1. The grid is rectangular, width and height don't exceed 100. Determine the perimeter of the island.

此题:输入一个二维矩阵表示地图,1表示陆地0表示水。岛屿是有1组成的四连通的图且只有一个岛屿。每个方格边长为1。求岛屿周长。

思路:此题的关键在于,相邻的1所处的方格边有重叠,周长只算外围的边。仔细观察每个1所处方格对周长的贡献,可以发现,有1相邻的那条边是不算入岛屿周长的。由此可得解题思路:遍历整个二维矩阵,每当遇到1,则count_1++(统计1的数目),然后对这个1进行上下左右试探,若有1相邻,则减去1条边,minus++(这条边对周长没贡献)。最后,由4*count_1得出所有1总共的边长,再减去对周长没贡献的边minus数,即得岛屿周长。

public int islandPerimeter(int[][] grid) {
int res=0;
int count_1=0;
int minus=0;
for(int i=0;i<grid.length;++i){
for(int j=0;j<grid[0].length;++j){
if(grid[i][j]==1){
count_1++;
//下
if(i-1>=0 && grid[i-1][j]==1){
minus++;
}
//右
if(j+1<grid[0].length && grid[i][j+1]==1){
minus++;
}
//上
if(i+1<grid.length && grid[i+1][j]==1){
minus++;
}
//左
if(j-1>=0 && grid[i][j-1]==1){
minus++;
}
}
}
}
res=4*count_1-minus;
return res;
}

8:Max Consecutive Ones

Given a binary array, find the maximum number of consecutive 1s in this array.

此题:给出一个元素只含1、0的数组,统计最长的连续1串的长度。

思路:用一个temp值保存当前最长的1连续串的长度。从头开始遍历数组,遇1则count++,然后比较当前count与temp,temp取大值。时刻更新temp,最后把temp返回。

这里有个坑:如果是遇0才把count与temp比较取大,然后count=0统计下一个连续的1串,这样的话不是时刻更新temp,若最长的连续1串一直延续到数组末尾。则temp是没机会更新的。

public int findMaxConsecutiveOnes(int[] nums) {
int temp=0;
int count=0;
for(int i:nums){
count = i==1?++count:0;
temp=Math.max(temp, count);
}
return temp;
}

9: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.

此题:博弈论的题。两人轮流取石头,每次1~3块,取到最后一块的算赢。你先取,问在n块石头的情况下你能不能赢?

通用的解题思路是:对于一堆n块的石头,每次能取1~m块。如果n=m+1,那么先取的无论怎么取,剩下的都<=m,后取的赢。由此可得,我们要做的就是,争取制造出这样的一种结果:在最后一次取时,刚好是m+1块给对方取,那么我们随后就可以一次取完。怎么达到呢?由于我们是先手,由n=(m+1)*r+s得出这堆石头是m+1的多少倍余多少块,然后我们先取走s块,则留下(m+1)*r块石头。此时,开启我们的交锋——每个回合都是他先取,而且面对的都是m+1的倍数。他无论怎么取(假如取k块),我们只需取走(m+1-k)块即可保持剩余石头数仍是m+1的倍数(即保持每个回合两人总共取走m+1个)......直到最后一个回合开始时,刚好剩下m+1块而且是他先手,那么他无论怎么取,我们都可以随后一次取完。则把问题转变为了求:n<=m||n=(m+1)*r+s(0<s<=m)这条式子是否成立,成立则我们赢,否则对手赢。(由此也可得博弈论类题目,都不是单纯的模拟推演结果,因为每个回合变数太大不可能推演完全的。而博弈是个零和游戏,总会有输赢,我们要做的就是找出必胜的情况,得出必胜的公式,然后由公式成立与否得结果

 public boolean canWinNim(int n) {
//注意边界:n<=m,先手的一次取完就赢了。而(m+1)*r+s(0<s<=m)不是为了求r,而是为了确保s不为0,使我们先手时能取s,否则就是对方利用这条公式对付我们了
//如果进一步,最快取多少次后获胜,则我们还要把r求出来。
if(n<=3||n%4!=0){
return true;
}
return false;
}

10:Find All Numbers Disappeared in an Array

Given an array of integers where 1 ≤ a[i] ≤ n (n = size of array), some elements appear twice and others appear once.

Find all the elements of [1, n] inclusive that do not appear in this array.

Could you do it without extra space and in O(n) runtime? You may assume the returned list does not count as extra space.

此题:给出一个n长数组,每个元素在1~n之间,且元素可以重复出现。求1~n这n个整数哪个不在数组中。

思路:这道题有性能限制,O(n)以内,所以不能用普通的嵌套循环来做的。由前面的题目的灵感,这种找位置(下标)类情况,绝对要用map啦!先遍历一次数组,每个元素作为key又作自身的value存入map。然后再遍历1~n这n个数字,由map.get(i)得不到value的说明不在数组中,list.add(i)。最后返回list即可。

public List<Integer> findDisappearedNumbers(int[] nums) {
List<Integer> list=new ArrayList<Integer>();
Map<Integer, Integer> map=new HashMap<Integer, Integer>();
for(int i:nums){
map.put(i, i);
}
for(int i=1;i<=nums.length;++i){
if(map.get(i)==null){
list.add(i);
}
}
return list;
}

【leetcode】solution in java——Easy2的更多相关文章

  1. 【leetcode】solution in java——Easy4

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6415011.html 16:Invert Binary Tree 此题:以根为对称轴,反转二叉树. 思路:看到 ...

  2. 【leetcode】solution in java——Easy3

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6412505.html 心得:看到一道题,优先往栈,队列,map,list这些工具的使用上面想.不要来去都是暴搜 ...

  3. 【leetcode】solution in java——Easy1

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6409067.html 1:Hamming distance The Hamming distance betw ...

  4. 【leetcode】solution in java——Easy5

    转载请注明原文地址: 21:Assign Cookies Assume you are an awesome parent and want to give your children some co ...

  5. 【Leetcode】Reorder List JAVA

    一.题目描述 Given a singly linked list L: L0→L1→…→Ln-1→Ln,reorder it to: L0→Ln→L1→Ln-1→L2→Ln-2→… You must ...

  6. 【Leetcode】Sort List JAVA实现

    Sort a linked list in O(n log n) time using constant space complexity. 1.分析 该题主要考查了链接上的合并排序算法. 2.正确代 ...

  7. 【LeetCode】Minimum Depth of Binary Tree 二叉树的最小深度 java

    [LeetCode]Minimum Depth of Binary Tree Given a binary tree, find its minimum depth. The minimum dept ...

  8. 【刷题】【LeetCode】007-整数反转-easy

    [刷题][LeetCode]总 用动画的形式呈现解LeetCode题目的思路 参考链接-空 007-整数反转 方法: 弹出和推入数字 & 溢出前进行检查 思路: 我们可以一次构建反转整数的一位 ...

  9. 【LeetCode】Permutations 解题报告

    全排列问题.经常使用的排列生成算法有序数法.字典序法.换位法(Johnson(Johnson-Trotter).轮转法以及Shift cursor cursor* (Gao & Wang)法. ...

随机推荐

  1. 美国罪案故事第一季/全集American Crime Story迅雷下载

    英文全名American Crime Story,第1季(2016)FX.本季看点:<美国罪案故事>以律师们的视角看待辛普森谋杀案. 本剧探索了案件背后各种混乱,以及案件双方的庭审策略,也 ...

  2. 浴血黑帮第一季/全集Peaky Blinders迅雷下载

    本季第一季Peaky Blinders Season 1 (2013)看点:<浴血黑帮>Peaky Blinders是从战后伯明翰地区走出的一个传奇黑帮家族,时间要追溯到1919年,家族成 ...

  3. win7下设置环境变量

    手工当然可以进行环境变量的设置,但是如果一个小组有需要设置固定环境变量的操作,这可能就会有点麻烦了,xp下设置环境变量比较简单,直接用set,win7下需要使用setx SETX XX_HOME &q ...

  4. 部署maven的一些要点、遇到的问题和心得体会

    maven的部署.遇到的问题和心得体会 2013-10-24 | 阅:  转:  |  分享         部署maven的一些要点.遇到的问题和心得体会 (图片看不了,可以下载doc文件) 一.  ...

  5. [转]cocos2d-x

    Cocos2d-x 是一个支持多平台的 2D 手机游戏引擎,使用 C++ 开发,基于OpenGL ES,基于Cocos2d-iphone,支持 WOPhone, iOS 4.1, Android 2. ...

  6. 6.2 dubbo在spring中自定义xml标签源码解析

    在6.1 如何在spring中自定义xml标签中我们看到了在spring中自定义xml标签的方式.dubbo也是这样来实现的. 一 META_INF/dubbo.xsd 比较长,只列出<dubb ...

  7. Groupnet, Subnet, IP Pool的概念

    Groupnet是OneFS 8.0新引入的network object,专门为了服务Multi-Tenancy而创建,目的是让OneFS可以同时地处理多套网络配置. 如果用户不需要使用multi-t ...

  8. ThinkPHP3.2.3使用分页

    首先要搞清楚的就是ThinkPHP3.2.3的分页类已经被移到了Think\Page.class.php,这是跟以前的版本有些不一样的,使用起来还是跟以前版本差不多,但是默认的效果不敢恭维,所以最好是 ...

  9. Java 读取 .properties 配置文件

    java 开发中,经常要读取 properties 配置文件,下面介绍几种读取方式: 1.基于 InputStream 读取配置文件 该方式的优点在于可以读取任意路径下的配置文件 Properties ...

  10. Java通信过程的中文乱码的解决

    J在基于Java的编程中,常常会碰到汉字的处里及显示的问题.比方一大堆乱码或问号. 这是由于JAVA中默认的编码方式是UNICODE.而中国人通常使用的文件和DB都是基于GB2312或者BIG5等编码 ...