# leetcode周期小结2

6.Z字形变换

P   A   H   N
A P L S I I G
Y   I   R


string convert(string text, int nRows);

convert("PAYPALISHIRING", 3) 应当返回 "PAHNAPLSIIGYIR" 。

class Solution {
public static String convert(String s, int numRows) {
int distance= 2*numRows-2;
int eachpart=distance-1;
if(numRows==1) return s;
else
return function(s,0,0,numRows,0);
}

public static String function(String s,int nowrow,int distance,int numRows,int nownum)  {
if(nowrow==numRows)      return "";
if(nownum>=s.length())   return  function(s,nowrow+1,2*numRows-2-2*(nowrow+1),numRows,nowrow+1);
if(nowrow==0)            return  s.charAt(nownum)+function(s,nowrow,2*numRows-2,numRows,nownum+2*numRows-2)  ;
if(nowrow==numRows-1)    return  s.charAt(nownum)+function(s,nowrow,2*numRows-2,numRows,nownum+2*numRows-2)  ;
return     s.charAt(nownum)+function(s,nowrow,2*numRows-2-distance,numRows,nownum+distance)  ;
}

}

public class Solution {
public String convert(String s, int numRows) {

char[] c=s.toCharArray();

StringBuilder[] sb=new StringBuilder[numRows];

for(int i=0;i<numRows;i++)
{
sb[i]=new StringBuilder();
}

int k=0;
while(k<s.length())
{
for(int i=0;i<numRows && k<s.length();i++)
{
sb[i].append(c[k++]);
}

for(int i=numRows-2;i>=1 && k<s.length();i--)
{
sb[i].append(c[k++]);
}

}

StringBuilder result=new StringBuilder();
for(int i=0;i<numRows;i++)
{
result.append(sb[i]);
}

return result.toString();

}
}

14.最长公共前缀

class Solution(object):
def longestCommonPrefix(self, strs):
if strs == []:
return ""
if len(strs)==1:
return strs[0]
left = 0
right = len(strs[0])
for i in range(0, len(strs)):

if right >len(strs[i]):
right = len(strs[i])
a = self.check(strs, i, left, right)
right=a
result=""
for i in range(0,right):
result=result+strs[0][i]
return result

def check(self, strs, pos, left, right):

for i in range(left, right):
if strs[pos][i] == strs[0][i]:
pass
else:
return i

return right

45.跳跃游戏2

class Solution:
def jump(self, nums):
"""
:type nums: List[int]
:rtype: int
"""
if len(nums)==1:
return 0
pos=0
temppos=0
counter=0
for i in range(0,len(nums)):
if nums[i]+i>=len(nums)-1:
if i<=nums[pos]+pos and i>pos:
return counter+2
else:
return counter+1

temppos=self.max(temppos,i,nums)

if i==nums[pos]+pos:
counter=counter+1
pos=temppos

def max(self, a, b,nums):
if nums[a]+a>nums[b]+b:
return a
else:

return b

495.提莫攻击

输入: [1,4], 2



输入: [1,2], 2

class Solution {
public static int findPoisonedDuration(int[] timeSeries, int duration) {
if(timeSeries.length==0)
return 0;
else
return run(timeSeries,duration,0);
}
static int p;
public static int run(int[] timeSeries,int duration,int now)
{

if(now>=timeSeries.length-1)
{ return duration;}
else
{  p=timeSeries[now+1]-timeSeries[now];}

return (p>=duration?duration:p)+run(timeSeries,duration,now+1);
}

}

605.种花问题

输入: flowerbed = [1,0,0,0,1], n = 1



输入: flowerbed = [1,0,0,0,1], n = 2



1. 数组内已种好的花不会违反种植规则。
2. 输入的数组长度范围为 [1, 20000]。
3. n 是非负整数，且不会超过输入数组的大小。

class Solution {
public static boolean canPlaceFlowers(int[] flowerbed, int n) {
if(n==0) return true;
if(flowerbed.length==1){if(flowerbed[0]==0&&n<=1) return true;else return false;}
for(int i=0;i<flowerbed.length;i++)
{
if(check(flowerbed,i)==true) {n--;flowerbed[i]=1;}
if(n==0) return true;
}
return false;

}

public static boolean check(int[] flowerbed, int pos){
if (pos == 0)
if (flowerbed[pos] == 0 && flowerbed[pos + 1] == 0)
return true;
else
return false;
if (pos == flowerbed.length - 1)
if (flowerbed[pos] == 0 & flowerbed[pos - 1] == 0)
return true;
else
return false;
if(flowerbed[pos] == 0 && flowerbed[pos + 1] == 0 && flowerbed[pos - 1] == 0)
return true;
else
return false;

}

}

bool canPlaceFlowers(int* flowerbed, int flowerbedSize, int n) {

int l = -2;
int r;
int sum = 0;
for (r = 0; r < flowerbedSize; ++r){
if (flowerbed[r] == 1){
sum = sum + floor((r-l-2) / 2.0);
l = r;
}
}
sum = sum + floor((flowerbedSize-l-1) / 2.0);

return sum>=n?true:false;
}