镜像站点
· 本页由第三方 GitHub 只读镜像提供,
非 GitHub 官方站点
,不接受任何登录或凭据输入。
前往 github.com
Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
LinXin04
LeetCode-Java
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
LeetCode-Java
/
CompareVersionNumber.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
49 lines (44 loc) · 1.47 KB
master
Breadcrumbs
LeetCode-Java
/
CompareVersionNumber.java
Copy path
Top
File metadata and controls
Code
Blame
49 lines (44 loc) · 1.47 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
/**
* 题目:比较版本号。
* 版本号一般用数字和小数点的形式表示,比如:2.2,1.1.1等。2.10代表2号大版本的第10个小版本,所以2.10比2.1大。
* 如果版本号1>版本号2,可以返回1;如果版本号1<版本号2,可以返回-1;否则返回0
* 解题思路:
* 首先我们将两个版本号放入数组中,然后逐位两两比较只要有一位大于或者小于便返回结果
*
*/
import java.util.Scanner;
public class CompareVersionNumber {
public static void main(String[] args) {
System.out.println("请输入字符串version1: ");
Scanner sc=new Scanner(System.in);
String version1=sc.nextLine();
System.out.println("请输入字符串version2:");
String version2=sc.nextLine();
Solution231 sl=new Solution231();
System.out.println("结果是:"+sl.compareVersion(version1, version2));
}
}
class Solution231
{
public int compareVersion(String version1,String version2)
{
if(version1==null || version2==null) return 0;
//split(" +")代表多个空格
//split("\\.")代表一个.
String[] temp1=version1.split("\\.");
String[] temp2=version2.split("\\.");
int len1=temp1.length;
int len2=temp2.length;
int i=0;
//要用或,因为短的那个版本号可以用0进行补充
while(i<len1 || i<len2)
{
int x1=i<len1?Integer.parseInt(temp1[i]):0;
int x2=i<len2?Integer.parseInt(temp2[i]):0;
if(x1>x2) return 1;
else if(x1<x2) return -1;
else i++;
}
return 0;
}
}
You can’t perform that action at this time.