Java C++題解leetcode816模糊坐標(biāo)示例
題目


思路:枚舉
- 既然要輸出每種可能了,那必然不能“偷懶”,就暴力枚舉咯;
- 在每個(gè)間隔處添加逗號(hào);
- 定義函數(shù)
decPnt(sta, end)分別列舉逗號(hào)左右兩邊的數(shù)能構(gòu)成的可能性;- 同樣在每個(gè)間隔添加小數(shù)點(diǎn);
- 注意兩種不合法的結(jié)構(gòu)——前導(dǎo)0和后綴0;
- 不要忘記無(wú)小數(shù)點(diǎn)的整數(shù)版本,
- 分別組合兩邊的不同可能性,根據(jù)要求各式加入答案。
Java
class Solution {
String str;
public List<String> ambiguousCoordinates(String s) {
str = s.substring(1, s.length() - 1); // 去除括號(hào)
int n = str.length();
List<String> res = new ArrayList<>();
for (int i = 0; i < n - 1; i++) { // 添加逗號(hào)
List<String> left = decPnt(0, i), right = decPnt(i + 1, n - 1);
for (var l : left) {
for (var r : right) {
res.add("(" + l + ", " + r + ")");
}
}
}
return res;
}
List<String> decPnt(int sta, int end) {
List<String> res = new ArrayList<>();
if (sta == end || str.charAt(sta) != '0') // 無(wú)小數(shù)
res.add(str.substring(sta, end + 1));
for (int i = sta; i < end; i++) { // 添加小數(shù)點(diǎn)
String inte = str.substring(sta, i + 1), dec = str.substring(i + 1, end + 1);
if (inte.length() > 1 && inte.charAt(0) == '0') // 前導(dǎo)0
continue;
if (dec.charAt(dec.length() - 1) == '0') // 后綴0
continue;
res.add(inte + "." + dec);
}
return res;
}
}

C++
class Solution {
public:
string str;
vector<string> ambiguousCoordinates(string s) {
str = s.substr(1, s.size() - 2); // 去除括號(hào)
int n = str.size();
vector<string> res;
for (int i = 0; i < n - 1; i++) { // 添加逗號(hào)
vector<string> left = decPnt(0, i), right = decPnt(i + 1, n - 1);
for (auto l : left) {
for (auto r : right) {
res.emplace_back("(" + l + ", " + r + ")");
}
}
}
return res;
}
vector<string> decPnt(int sta, int end) {
vector<string> res;
if (sta == end || str[sta] != '0') // 無(wú)小數(shù)
res.emplace_back(str.substr(sta, end - sta + 1));
for (int i = sta; i < end; i++) { // 添加小數(shù)點(diǎn)
string inte = str.substr(sta, i - sta + 1), dec = str.substr(i + 1, end - i);
if (inte.size() > 1 && inte[0] == '0') // 前導(dǎo)0
continue;
if (dec.back() == '0') // 后綴0
continue;
res.emplace_back(inte + "." + dec);
}
return res;
}
};

Rust
impl Solution {
pub fn ambiguous_coordinates(s: String) -> Vec<String> {
let stri = &s[1.. s.len() - 1];
let n = stri.len();
let mut res = vec![];
for i in 0..n-1 {
for l in Self::decPnt(stri, 0, i) {
for r in Self::decPnt(stri, i + 1, n - 1) {
res.push(format!("({}, {})", l, r));
}
}
}
res
}
fn decPnt(stri: &str, sta: usize, end: usize) -> Vec<String> {
let mut res = vec![];
if sta == end || &stri[sta..sta+1] != "0" { // 無(wú)小數(shù)
res.push(format!("{}", &stri[sta..end + 1]));
}
for i in sta..end { // 添加小數(shù)點(diǎn)
let (inte, dec) = (&stri[sta..i + 1], &stri[i + 1.. end + 1]);
if inte.len() > 1 && inte.starts_with("0") { // 前導(dǎo)0
continue;
}
if (dec.ends_with("0")) { // 后綴0
continue;
}
res.push(format!("{}.{}", inte, dec));
}
res
}
}

總結(jié)
也算是簡(jiǎn)單模擬題吧,收獲在于學(xué)到了一些快速定位字符串首末的小方法。
以上就是Java C++題解leetcode816模糊坐標(biāo)示例的詳細(xì)內(nèi)容,更多關(guān)于Java C++題解模糊坐標(biāo)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
java中List刪除時(shí)需要的注意事項(xiàng)
最近在利用java中的LIST在刪除時(shí)發(fā)現(xiàn)了一個(gè)錯(cuò)我,通過(guò)查找相關(guān)的資料終于解決了,覺(jué)著有必要分享處理給同樣遇到這個(gè)問(wèn)題的朋友參考,下面這篇文章主要介紹了java中List刪除時(shí)需要的注意事項(xiàng),需要的朋友可以一起來(lái)看看吧。2017-01-01
Gradle構(gòu)建多模塊項(xiàng)目的方法步驟
這篇文章主要介紹了Gradle構(gòu)建多模塊項(xiàng)目的方法步驟,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
解讀maven項(xiàng)目中Tomcat10與JSTL的問(wèn)題匯總(Debug親身經(jīng)歷)
這篇文章主要介紹了解讀maven項(xiàng)目中Tomcat10與JSTL的問(wèn)題匯總(Debug親身經(jīng)歷),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Spring boot實(shí)現(xiàn)數(shù)據(jù)庫(kù)讀寫(xiě)分離的方法
本篇文章主要介紹了Spring boot實(shí)現(xiàn)數(shù)據(jù)庫(kù)讀寫(xiě)分離的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-01-01
Java中BeanUtils.copyProperties基本用法與小坑
本文主要介紹了Java中BeanUtils.copyProperties基本用法與小坑,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
Java Socket實(shí)現(xiàn)猜數(shù)字小游戲
這篇文章主要為大家詳細(xì)介紹了Java Socket實(shí)現(xiàn)猜數(shù)字小游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-09-09
Java NIO寫(xiě)大文件對(duì)比(win7和mac)
這篇文章主要介紹了Java NIO寫(xiě)大文件對(duì)比(win7和mac),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07

