面试问题浓缩总结 面试问题浓缩总结
  • Go
  • Java
  • C/C++
  • JavaScript/HTML
  • MySQL
  • Redis
  • MongoDB
  • 操作系统
  • 计算机网络
  • spring全家桶
  • mybatis
  • 中间件
  • 软件相关
  • 系统相关
  • 算法
  • 数据结构
  • 设计模式
  • CMU硕士经典100题
  • 剑指offer
  • 重点手撕代码
  • 程序员面试金典
  • 3月
  • 4月
  • 智力题
  • 业务问题
  • 一些技术
  • 安全相关
APP下载 (opens new window)
GitHub (opens new window)
  • Go
  • Java
  • C/C++
  • JavaScript/HTML
  • MySQL
  • Redis
  • MongoDB
  • 操作系统
  • 计算机网络
  • spring全家桶
  • mybatis
  • 中间件
  • 软件相关
  • 系统相关
  • 算法
  • 数据结构
  • 设计模式
  • CMU硕士经典100题
  • 剑指offer
  • 重点手撕代码
  • 程序员面试金典
  • 3月
  • 4月
  • 智力题
  • 业务问题
  • 一些技术
  • 安全相关
APP下载 (opens new window)
GitHub (opens new window)
  • 算法

  • 数据结构

  • 设计模式

  • CMU硕士经典100题

  • 剑指offer

    • 3-10
    • 11-20
    • 21-30
    • 31-40
    • 41-50
    • 51-60
    • 61-70
      • 61.扑克牌中的顺子
        • 解法一:集合Set+遍历
        • 解法二: 排序+遍历
      • 62.圆圈中最后剩下的数字
        • 解法一: 动态规划
      • 63.股票的最大利润
      • 解法一: 动态规划
      • 64.求1+2+...+n
      • 解法一: 递归
      • 65.不用加减乘除做加法
        • 解法一:位运算
      • 66.构建乘积数组
        • 解法一:表格分区
      • 67.把字符串转换成整数
  • 重点手撕代码

  • 程序员面试

  • CodeTop企业题库

  • 笔试题目

  • 算法和数据结构
  • 剑指offer
小游
2021-03-29

61-70

# 61.扑克牌中的顺子

剑指 Offer 61. 扑克牌中的顺子 - 力扣(LeetCode) (leetcode-cn.com) (opens new window)

image-20210409201818768

没看懂这题啥意思。。。。我怎么这么菜啊

# 解法一:集合Set+遍历

首先我们的题大小王是可以重复出现的,而其他的不可以。然后就是因为我们只有5张牌,如果这5张牌的最大值和最小值相差要小于5才满足顺子的条件(为什么我们不判断数组是否递增呢,因为我们牌的顺序不一定是递增的。)

func isStraight(nums []int) bool {
   repeat:=make(map[int]bool)
   max:=0;min:=14
   for _,v:=range nums{
      // 跳过大小王
      if v == 0 { continue }
      // 更新一下最大牌和最小牌
      max=Max(max,v)
      min=Min(min,v)
      // 判断是否有重复元素
      if _, ok := repeat[v]; ok {
         return false
      }
      // 把当前排加入set中
      repeat[v] = true
   }
   // 判断是否可以构成顺子
   return max-min<5
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

# 解法二: 排序+遍历

这个解法也比较巧妙,我们可以先对数组进行排序,这样,大小王就会在前面,然后我们统计大小王的数目,确定我们最小值的位置(因为我们排了序,所以我们判断相邻的元素是否相等,如果相等,那么我们直接返回true)

func isStraight(nums []int) bool {
   joker:=0
   sort.Ints(nums)
   for i := 0; i < 4; i++ {
      if nums[i] == 0 { 
         joker++ 
      } else if nums[i] == nums[i+1] { 
         return false
      }
   }
   return nums[4]-nums[joker] < 5
}
1
2
3
4
5
6
7
8
9
10
11
12

# 62.圆圈中最后剩下的数字

剑指 Offer 62. 圆圈中最后剩下的数字 - 力扣(LeetCode) (leetcode-cn.com) (opens new window)

image-20210409203728234

这题我本来还想着用循环。。。但是看了一下n的范围,我选择放弃了。。。

# 解法一: 动态规划

这题实际上是有数学规律的,我这里就先暂时不证明了(没时间哈哈)

image-20210410093209472

没错,代码就是这么点

func lastRemaining(n int, m int) int {
   ans:=0
   for i := 2; i <= n; i++ {
      ans = (ans+m)%i
   }
   return ans
}
1
2
3
4
5
6
7

# 63.股票的最大利润

剑指 Offer 63. 股票的最大利润 - 力扣(LeetCode) (leetcode-cn.com) (opens new window)

image-20210410093337535

这题我做过,所以我就不多说了,直接贴代码

# 解法一: 动态规划

其实就是在最低价格的时候买入,然后最高价格卖出就可以了

func maxProfit(prices []int) int {
   if len(prices) == 0 {
      return 0
   }
   profile:=0;sell:=prices[0]
   for i:=0;i<len(prices);i++ {
      // 判断当前价格是否比sell还低
      if prices[i] < sell {
         sell = prices[i]
      } else if prices[i]-sell > profile {
         profile = prices[i]-sell
      }
   }
   return profile
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 64.求1+2+...+n

剑指 Offer 64. 求1+2+…+n - 力扣(LeetCode) (leetcode-cn.com) (opens new window)

image-20210410094032850

这题我感觉就是纯粹的数学技巧了。。

# 解法一: 递归

因为我们不能用乘除,判断,所以我们只能使用递归来进行判断了

var res = 0
var x bool
// 计算
func sumNums(n int) int {
   // 计算时我们需要把res置为0,避免
   res = 0
   return count(n)
}

// 开始计算
func count(n int) int {
   x=n>1 && sumNums(n-1) >0
   res += n
   return res
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

# 65.不用加减乘除做加法

剑指 Offer 65. 不用加减乘除做加法 - 力扣(LeetCode) (leetcode-cn.com) (opens new window)

image-20210410104437092

这东西一看就是用位运算的,可惜我不会哈哈哈哈

# 解法一:位运算

做这种题目需要对位运算有深刻的认识

image-20210410104955653

实际代码如下

func add(a int, b int) int {
   for b != 0 {
      c:=(a&b)<<1
      a^=b
      b=c
   }
   return a
}
1
2
3
4
5
6
7
8

# 66.构建乘积数组

剑指 Offer 66. 构建乘积数组 - 力扣(LeetCode) (leetcode-cn.com) (opens new window)

image-20210410105129613

确认过眼神,又是一道不会的题,,,

# 解法一:表格分区

因为其中 B[i] 的值是数组 A 中除了下标 i 以外的元素的积,所以我们就可以看成下面这样的,把当前下标i置为1,然后分成了两个区域,下三角和上三角,我们可以先计算下三角,因为当前位置的值和前一个位置有关,然后再计算上三角,然后在和当前位置相乘。

image-20210410110710917

func constructArr(a []int) []int {
   if len(a) == 0 {
      return []int{}
   }
   // 初始化数组
   b:=make([]int,len(a))
   // 先把b[0]置为1
   b[0] = 1
   
   tmp:=1
   // 这里是计算下三角,
   for i := 1; i < len(a); i++ {
      b[i] = b[i-1]*a[i-1]
   }
   // 这里我们计算上三角,然后计算得到的tmp去乘
   for i := len(a) - 2; i >= 0; i-- {
      tmp*=a[i+1]
      b[i]*=tmp
   }
   return b
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

# 67.把字符串转换成整数

剑指 Offer 67. 把字符串转换成整数 - 力扣(LeetCode) (leetcode-cn.com) (opens new window)

image-20210410110952720

这题怎么说呢,难倒是不难,问题在于如何处理边界条件,这东西细节还是很多的

class Solution {
    public int strToInt(String str) {
        int res = 0, bndry = Integer.MAX_VALUE / 10;
        int i = 0, sign = 1, length = str.length();
        if(length == 0) return 0;
        while(str.charAt(i) == ' ')
            if(++i == length) return 0;
        if(str.charAt(i) == '-') sign = -1;
        if(str.charAt(i) == '-' || str.charAt(i) == '+') i++;
        for(int j = i; j < length; j++) {
            if(str.charAt(j) < '0' || str.charAt(j) > '9') break;
            if(res > bndry || res == bndry && str.charAt(j) > '7')
                return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            res = res * 10 + (str.charAt(j) - '0');
        }
        return sign * res;
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
编辑 (opens new window)
上次更新: 2021/04/10, 14:27:26
51-60
链表

← 51-60 链表→

Theme by Vdoing | Copyright © 2021-2021 小游
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式