Leetcode——93. Restore IP Addresses

2024-02-02 01:32
文章标签 leetcode ip 93 restore addresses

本文主要是介绍Leetcode——93. Restore IP Addresses,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目

Given a string containing only digits, restore it by returning all possible valid IP address combinations.

For example:
Given “25525511135”,

return [“255.255.11.135”, “255.255.111.35”]. (Order does not matter)
https://leetcode.com/problems/restore-ip-addresses/

解答

两个常用的函数:

to_string():转化为字符串
stoi():把一个字符串转化为整数

蛮力算法
如下:

class Solution {
public:vector<string> restoreIpAddresses(string s) {vector<string>res;for(int a=1;a<=3;a++)//截取长度for(int b=1;b<=3;b++)for(int c=1;c<=3;c++)for(int d=1;d<=3;d++){if(a+b+c+d==s.length()){int A=stoi(s.substr(0,a));int B=stoi(s.substr(a,b));int C=stoi(s.substr(a+b,c));int D=stoi(s.substr(a+b+c,d));if(A<=255&&B<=255&&C<=255&&D<=255){if((to_string(A)+to_string(B)+to_string(C)+to_string(D)).length()==s.length())//这一行需要添加的原因是:是为了排除以0开头的数字,比如,10.02.1.1(这种情况是不满足的)res.push_back(s.substr(0,a)+"."+s.substr(a,b)+"."+s.substr(a+b,c)+"."+s.substr(a+b+c,d));}}}return res; }
};

或者利用DFS思想

//DFS思想
class Solution {
public:vector<string> restoreIpAddresses(string s) {vector<string>res;restoreIp(res,s,"",0,0);return res;}private:void restoreIp(vector<string>&res,string s,string restored,int count,int idx)//分别是存储结果,原始的字符串s,存储的s,count来计数,idx来表示移动到的下标{if(count>4) return ;if(count==4&&idx==s.length()) res.push_back(restored);for(int i=1;i<=3;i++){if(idx+i>s.length()) break;//+i之后超出,就直接break,后续不再验证string temp=s.substr(idx,i);if((temp[0]=='0'&&i>1)||stoi(temp)>255) continue;//不满足情况,直接跳转到i++restoreIp(res,s,restored+temp+(count==3?"":"."),count+1,idx+i);}}
};

这篇关于Leetcode——93. Restore IP Addresses的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/669082

相关文章

SQL Server中,用Restore DataBase把数据库还原到指定的路径

restore database 数据库名 from disk='备份文件路径' with move '数据库文件名' to '数据库文件放置路径', move '日志文件名' to '日志文件存放置路径' Go 如: restore database EaseWe from disk='H:\EaseWe.bak' with move 'Ease

[vivado][IP核]FFT

刘东华的IP核详解: 1、 2、

[vivado][IP核]DDS

刘东华的IP核详解: 1、 这里的是指IP核配置中的相位数据的宽度。 2、 实际使用此IP核时并没有“频率分辨率”可以配,是靠改变来变的。 3、 4、 5、 数据输出的ready在数据正式输出时才会有。 自己仿真: 使用SIN/COS LUT only的模式,使用一个累加器作为相位输入,不知怎么,输出为X。

[ip核][vivado]aurora

Xapp1193:  discovered:1)并不是所有芯片都支持aurora.xc7z010就没有。                     2)XDC文件的指令-允许未约束的引脚的存在:                 set_property BITSTREAM.General.UnconstrainedPins {Allow} [current_design] PG046

[ip核][vivado]Block Menory Gennerator 学习

<刘东华的xilinx系列FPGA芯片IP核详解>读书摘录: 1. 2. 3.

[ip核][vivado]FIFO 学习

<xlinx FPGA应用进阶 通用IP核详解和设计开发>读书摘录: 1.        2.3.仿真模型 特点总结:1)复位后会有busy状态,需要等待wr_rst_busy信号低电平后才能正常写入                  2)prog_full信号的高电平长度可调                  3)仿真中的读状态很奇怪,并没有正常读取,都是XXX的状态。 所用的te

LeetCode--231 2的幂

题目 给定一个整数,编写一个函数来判断它是否是 2 的幂次方。 示例 示例 1:输入: 1输出: true解释: 20 = 1示例 2:输入: 16输出: true解释: 24 = 16示例 3:输入: 218输出: false class Solution {public:bool isPowerOfTwo(int n) {if (n <= 0) return fals

LeetCode--234 回文链表

题目 请判断一个链表是否为回文链表。 示例 示例 1:输入: 1->2输出: false示例 2:输入: 1->2->2->1输出: true /*** Definition for singly-linked list.* struct ListNode {* int val;* ListNode *next;* ListNode(int x) : val

LeetCode--220 存在重复元素 III

题目 给定一个整数数组,判断数组中是否有两个不同的索引 i 和 j,使得 nums [i] 和 nums [j] 的差的绝对值最大为 t,并且 i 和 j 之间的差的绝对值最大为 ķ。 示例 示例 1:输入: nums = [1,2,3,1], k = 3, t = 0输出: true示例 2:输入: nums = [1,0,1,1], k = 1, t = 2输出: true示例

LeetCode--217 存在重复元素

题目 给定一个整数数组,判断是否存在重复元素。如果任何值在数组中出现至少两次,函数返回 true。如果数组中每个元素都不相同,则返回 false。 示例 示例 1:输入: [1,2,3,1]输出: true示例 2:输入: [1,2,3,4]输出: false示例 3:输入: [1,1,1,3,3,4,3,2,4,2]输出: true class Solution {p