链接:搜索旋转排序数组target数下标
解题思路:
1、暴力破解
C++:
class Solution {
public:
int search(vector& nums, int target) {
for(int i=0;i
题目二:
链接:搜索旋转列表找target下标
解题思路:
2、借助函数index
python:
class Solution(object):
def search(self, nums, target):
"""
:type nums: List[int]
:type target: int
:rtype: bool
"""
try:
s=nums.index(target)
except ValueError as e:
s=-1
return s!=-1
题目三:
链接:查找旋转链表最小值
解题思路:
暴力做法
Java:
class Solution {
public int findMin(int[] nums) {
int mi=5005;
for(int i=0;i
题目四:
链接:爬楼梯
解题思路:
每次只能爬1,2个阶梯:
为1个阶梯时,我们有1种方法;1
为2个阶梯时,我们有2种方法:1+1,2
为3个阶梯时,我们有3种方法:1+1+1,2+1,1+2
为4个阶梯时,我们有5种方法:1+1+1+1,1+2+1,1+1+2,2+1+1,2+2
To sum up 就是我们所学的斐波拉契:f[i]=f[i-1]+f[i-2]
Java:
class Solution {
public int climbStairs(int n) {
int data[] =new int[10000];
data[0]=data[1]=1;
for(int i=2;i<=n;i++){
data[i]=data[i-1]+data[i-2];
}
return data[n];
}
}
题目五:
链接::斐波拉契数列
解题思路:
通过递归实现斐波拉契数列
JAVA:
class Solution {
public int fib(int n) {
if(n==0)
return 0;
else if(n==1)
return 1;
else
return fib(n-1)+fib(n-2);
}
}
题目六:
链接:差的绝对值为 K 的数对数目
解题思路:
暴力穷举
Java:
class Solution {
public int countKDifference(int[] nums, int k) {
int s=0;
for(int i=0;i
题目七:
链接:猜数字
解题思路:
C++:
class Solution {
public int game(int[] guess, int[] answer) {
int s=0;
for(int i=0;i<3;i++){
if (guess[i]==answer[i]){
s++;
}
}
return s;
}
}



