5天前
import java.util.*;
public class Main {
public static void main(String[] args) {

Scanner input=new Scanner(System.in);
while(input.hasNext()){

int n=input.nextInt();
if(n==0)
{
break;
}

int arr[]=new int[n];
int result=0;
int side=0;
for (int i=0;i<n;i++)
{
arr[i]=input.nextInt();

}
for (int i=0;i<n;i++)
{
if (arr[i]>side)
{
result+=(arr[i]-side)*6+5;

}
else{
result+=(side-arr[i])*4+5;

}
side=arr[i];

}
System.out.println(result);
}

}
}


9天前

class Solution {
public int maxProductAfterCutting(int length)
{

    if (length<2)
return 0;
if (length==2)
return 1;
if(length==3)
return 2;
int timeof3=length/3;
if(length-timeof3*3==1)
timeof3--;
int timeof2=(length-timeof3*3)/2;
int result=int(pow(3,timeof3)*pow(2,timeof2));
return result;


}
}

9天前

1.这个题目允许修改数组内部元素
2.这个题目和第十四题的数据范围有区别，但是为什么第14题可以ac但是这道题目不能ac呢？

class Solution {
public int duplicateInArray(int[] nums) {
Set<Integer> hSet = new HashSet<Integer>();

for (int num: nums){

if (num >= 0 && num <= nums.length-1){
return num;

}

}

return -1;
}
}


11个月前

true

‘str’ object does not support item assignment

# -*- coding:utf-8 -*-
class Solution:
def hasPath(self, matrix, rows, cols, path):
'''

:param matrix: 表示矩阵
:param rows: 行数
:param cols: 列数
:param path: 待测字符串
:return:返回待测字符串是否在matrix里面
'''
for i in range(rows):
for j in range(cols):
if self.dfs(matrix,path,0,i,j):
return True
return False
def dfs(self,matrix,path,u,x,y):
'''

:param matrix:矩阵
:param path:待测字符串
:param u:待测字符串里面的字符
:param x:行下标
:param y:列下标
:return:
'''
memo=matrix[x][y]
if matrix[x][y]!=path[u]:
return False
#出口,表示u达到了path字符串里面的下标最后一个，那么说明找到符合的字符串了，返回True
if u==len(path)-1:
return True
#进行坐标变换，按照上右下左的形式，
dx=[-1,0,1,0]
dy=[0,1,0,-1]
matrix[x][y]='*'
for i in range(4):
a=x+dx[i]
b=y+dy[i]

if a>=0 and a<len(matrix) and b>=0 and b<len(matrix[a]):
if self.dfs(matrix,path,u+1,a,b):
return True
#状态还原

matrix[x][y]=memo
return False



2019-06-16 12:27

from collections import deque
class Solution(object):
def movingCount(self, threshold, rows, cols):
if not rows or not cols:
return 0
res=1
memo=set()#用来去重

# 初始化
q=deque([(0,0)])#采用双端队列
#采用上右下左的顺序对于坐标进行移动
dx=[-1,0,1,0]
dy=[0,1,0,-1]
while q:
x,y=q.popleft()
for i in range(4):
#开始移动坐标
xx=x+dx[i]
yy=y+dy[i]
if self.is_valid(xx,yy,rows,cols,threshold) and (xx, yy) not in memo:
res+=1
q.append((xx,yy))
return res

def get_sum(self,x):
res=0
while  x:
res+=x%10
x=x//10

return res
def is_valid(self,x,y,m,n,k):
"""

:param x: 表示当前行index
:param y: 表示当前列index
:param m:表示行范围
:param n:列范围
:param k:行列之和最大值限制
:return:
"""
return 0<=x<m and 0<=y<n and self.get_sum(x)+self.get_sum(y)<=k



2019-06-12 18:16

class Solution(object):
def Power(self, a, b):
"""
:type base: float
:type exponent: int
:rtype: float
"""
is_negative=0
res=1
if b<0:
b=-b
is_negative=1
while b:
res*=a
b-=1
if is_negative:
res=1/res
return res



2019-06-12 17:49
class Solution(object):
def maxProductAfterCutting(self,length):
"""
题目解析：
用来找一个剪绳子的方案，使得减绳子之后的每段乘积大于当前绳子总长度
解法：n表示绳子总长度，m表示剪绳子的段数
1.如果绳子总长度<4，那么减绳子之后的乘积小于绳子总长度
2.如果绳子总长度=4，那么可以将绳子减为2段，此时每段乘积和总长度相等
3.如果绳子总长度>=5，那么剪绳子的乘积肯定存在某个值大于绳子总长，可以证明2(n-2)>n并且3(n-3)>n。而且3(n-3)>=2(n-2)。所以我们应该尽可能地多剪长度为3的绳子段，长度为2的绳子最多2段，不要留绳子长度为1的

:type length: int
:rtype: int
"""
# 边界判断
if length<2:return 0
if length==2:return 1
if length==3:return 2

#其他情况，如果总的绳子长度=4，那么效果一样，最大乘积都是4
#如果绳子长度>=5,那么需要尽可能的多剪成长度为3的子绳子段,然后长度为2的绳子最多2段，不要留绳子长度为1的
timesOf3=length//3
if (length-timesOf3*3)==1:#length=4,7,10,如果是10=3*3*3*1或者3*（）
timesOf3-=1
timeOf2=(length-timesOf3*3)//2
result=pow(3,timesOf3)*pow(2,timeOf2)
return result


2019-05-26 15:41
# -- coding: utf-8 --

# 斐波那契数列
class solution:
def Fibonacci( n):
a,b=0,1

while n:
c=a+b
a=b
b=c
n-=1
return a

def Fibonacci_2(self,n):
if n<=0:
return 0
if n==1 or n==2:
return 1
else:
return self.Fibonacci(n-1)+self.Fibonacci(n-2)


2019-05-26 15:14

class MyQueue(object):

def __init__(self):
"""
Initialize your data structure here.
"""
self.stack1=[]
self.tmp_stack=[]

def push(self, x):
"""
Push element x to the back of queue.
:type x: int
:rtype: void
"""
self.stack1.append(x)

def pop(self):
"""
Removes the element from in front of queue and returns that element.
:rtype: int
"""
while self.stack1:
self.tmp_stack.append(self.stack1.pop())

result=self.tmp_stack.pop()
while self.tmp_stack:
self.stack1.append(self.tmp_stack.pop())
return result

def peek(self):
"""
Get the front element.
:rtype: int
"""
return self.stack1[0]

def empty(self):
"""
Returns whether the queue is empty.
:rtype: bool
"""
return not self.stack1

# Your MyQueue object will be instantiated and called as such:
# obj = MyQueue()
# obj.push(x)
# param_2 = obj.pop()
# param_3 = obj.peek()
# param_4 = obj.empty()


2019-05-26 13:22

# Definition for singly-linked list.
# class ListNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.next = None
class Solution(object):
"""
:rtype: List[int]
"""
return []
# pre存储的是逆置之后的链表节点，
# lat是cur的后面一个节点
pre=None

count=0
# 遍历到结尾
while cur:
lat=cur.next
cur.next=pre
# pre向后移动
pre=cur
# ｃｕｒ向后移动
cur=lat
count+=1
result=[]
for i in range(count):
result.append(pre.val)
pre=pre.next
return result

# 返回链表头指针