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 }}
chenwenhang
AlgorithmPractice
Repository navigation
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
AlgorithmPractice
/
src
/
swordPointOffer
/
StringCompression.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
67 lines (44 loc) · 1.39 KB
master
Breadcrumbs
AlgorithmPractice
/
src
/
swordPointOffer
/
StringCompression.java
Copy path
Top
File metadata and controls
Code
Blame
67 lines (44 loc) · 1.39 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
package swordPointOffer;
/**
* @Author: Wenhang Chen
* @Description:字符串压缩。利用字符重复出现的次数,编写一种方法,实现基本的字符串压缩功能。比如,字符串aabcccccaaa会变为a2b1c5a3。若“压缩”后的字符串没有变短,则返回原先的字符串。你可以假设字符串中只包含大小写英文字母(a至z)。 示例1:
* <p>
* 输入:"aabcccccaaa"
* 输出:"a2b1c5a3"
* 示例2:
* <p>
* 输入:"abbccd"
* 输出:"abbccd"
* 解释:"abbccd"压缩后为"a1b2c2d1",比原字符串长度更长。
* 提示:
* <p>
* 字符串长度在[0, 50000]范围内。
* @Date: Created in 21:40 5/12/2020
* @Modified by:
*/
public class StringCompression {
public String compressString(String S) {
if (S == null || S.length() < 1)
return S;
char ch = S.charAt(0);
int num = 0;
StringBuilder st = new StringBuilder();
for (int i = 0; i < S.length(); i++) {
if (ch == S.charAt(i)) {
num++;
} else {
st.append(ch);
st.append(num);
ch = S.charAt(i);
num = 1;
}
}
st.append(ch);
st.append(num);
if (st.length() < S.length()) {
return st.toString();
} else {
return S;
}
}
}
You can’t perform that action at this time.