Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

字符串中第二大的数字 #125

Open
yankewei opened this issue Mar 22, 2021 · 1 comment
Open

字符串中第二大的数字 #125

yankewei opened this issue Mar 22, 2021 · 1 comment
Labels
字符串 题目类型为字符串 简单 题目难度为简单

Comments

@yankewei
Copy link
Owner

给你一个混合字符串 s ,请你返回 s 中 第二大 的数字,如果不存在第二大的数字,请你返回 -1 。

混合字符串 由小写英文字母和数字组成。

示例 1:

输入:s = "dfa12321afd"
输出:2
解释:出现在 s 中的数字包括 [1, 2, 3] 。第二大的数字是 2 。

示例 2:

输入:s = "abc1111"
输出:-1
解释:出现在 s 中的数字只包含 [1] 。没有第二大的数字。

提示:

  • 1 <= s.length <= 500
  • s 只包含小写英文字母和(或)数字。
@yankewei yankewei added 字符串 题目类型为字符串 简单 题目难度为简单 labels Mar 22, 2021
@yankewei
Copy link
Owner Author

遍历一遍即可
func secondHighest(s string) int {
ret := [2]int{-1,-1}
for i := 0; i < len(s); i++ {
diff := int(s[i] - '0')
if diff <= 9 {
if diff > ret[0] {
ret[1] = ret[0]
ret[0] = diff
} else if diff > ret[1] && diff != ret[0]{
ret[1] = diff
}
}
}
return ret[1]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
字符串 题目类型为字符串 简单 题目难度为简单
Projects
None yet
Development

No branches or pull requests

1 participant