针对[SwiftWeeklyContest126]LeetCode1003.检查替换后的词是否有效|CheckIfWordIsValidAfterSubstitutions这个问题,本篇文章进行了详细
针对[Swift Weekly Contest 126]LeetCode1003. 检查替换后的词是否有效 | Check If Word Is Valid After Substitutions这个问题,本篇文章进行了详细的解答,同时本文还将给你拓展$word = new COM("word.application") or die("Unable to instantiate Word");、CSS中“word-break:break-all”与“word-wrap:break-word”之间的区别是什么?、Jacob 模板替换生成 word 文件、word 合并、word 转 pdf 文件、LeetCode-1003 Check If Word Is Valid After Substitutions Solution (with Java)等相关知识,希望可以帮助到你。
本文目录一览:- [Swift Weekly Contest 126]LeetCode1003. 检查替换后的词是否有效 | Check If Word Is Valid After Substitutions
- $word = new COM("word.application") or die("Unable to instantiate Word");
- CSS中“word-break:break-all”与“word-wrap:break-word”之间的区别是什么?
- Jacob 模板替换生成 word 文件、word 合并、word 转 pdf 文件
- LeetCode-1003 Check If Word Is Valid After Substitutions Solution (with Java)
[Swift Weekly Contest 126]LeetCode1003. 检查替换后的词是否有效 | Check If Word Is Valid After Substitutions
We are given that the string "abc"
is valid.
From any valid string V
,we may split V
into two pieces X
and Y
such that X + Y
(X
concatenated with Y
) is equal to V
. (X
or Y
may be empty.) Then, X + "abc" + Y
is also valid.
If for example S = "abc"
,then examples of valid strings are: "abc","aabcbc","abcabc","abcabcababcc"
. Examples of invalid strings are: "abccba"
, "ab"
, "cababc"
, "bac"
.
Return true
if and only if the given string S
is valid.
Example 1:
Input: "aabcbc"
Output: true Explanation: We start with the valid string "abc". Then we can insert another "abc" between "a" and "bc",resulting in "a" + "abc" + "bc" which is "aabcbc".
Example 2:
Input: "abcabcababcc"
Output: true Explanation: "abcabcabc" is valid after consecutive insertings of "abc". Then we can insert "abc" before the last letter,resulting in "abcabcab" + "abc" + "c" which is "abcabcababcc".
Example 3:
Input: "abccba"
Output: false
Example 4:
Input: "cababc"
Output: false
Note:
1 <= S.length <= 20000
-
S[i]
is‘a‘
,‘b‘
,or‘c‘
给定有效字符串 "abc"
。
对于任何有效的字符串 V
,我们可以将 V
分成两个部分 X
和 Y
,使得 X + Y
(X
与 Y
连接)等于 V
。(X
或 Y
可以为空。)那么,X + "abc" + Y
也同样是有效的。
例如,如果 S = "abc"
,则有效字符串的示例是:"abc"
,"aabcbc"
,"abcabc"
,"abcabcababcc"
。无效字符串的示例是:"abccba"
,"ab"
,"cababc"
,"bac"
。
如果给定字符串 S
有效,则返回 true
;否则,返回 false
。
示例 1:
输入:"aabcbc" 输出:true 解释: 从有效字符串 "abc" 开始。 然后我们可以在 "a" 和 "bc" 之间插入另一个 "abc",产生 "a" + "abc" + "bc",即 "aabcbc"。
示例 2:
输入:"abcabcababcc" 输出:true 解释: "abcabcabc" 是有效的,它可以视作在原串后连续插入 "abc"。 然后我们可以在最后一个字母之前插入 "abc",产生 "abcabcab" + "abc" + "c",即 "abcabcababcc"。
示例 3:
输入:"abccba" 输出:false
示例 4:
输入:"cababc" 输出:false
提示:
1 <= S.length <= 20000
-
S[i]
为‘a‘
、‘b‘
、或‘c‘
1 class Solution { 2 func isValid(_ S: String) -> Bool { 3 var n:Int = S.count 4 var arr:[Character] = Array(S) 5 var h:[Int] = [Int](repeating:0,count:3) 6 for i in 0..<n 7 { 8 h[arr[i].ascii - 97] += 1 9 } 10 if !S.contains("abc") 11 { 12 return false 13 } 14 if h[0] != h[1] || h[1] != h[2] 15 { 16 return false 17 } 18 var cc:[Int] = [Int](repeating:0,count:3) 19 for i in 0..<n 20 { 21 cc[arr[i].ascii - 97] += 1 22 if cc[0] < cc[1] || cc[1] < cc[2] 23 { 24 return false 25 } 26 } 27 return true 28 } 29 } 30 31 //Character扩展 32 extension Character 33 { 34 //转ASCII整数值(定义小写为整数值) 35 var ascii: Int { 36 get { 37 return Int(self.unicodeScalars.first!.value) 38 } 39 } 40 }
$word = new COM("word.application") or die("Unable to instantiate Word");
$word = new COM("word.application") or die("Unable to instantiate Word");
以上代码为什么在 PHP 环境上不能执行???
给位高手。。。。。。
CSS中“word-break:break-all”与“word-wrap:break-word”之间的区别是什么?
我目前想知道两者之间有什么区别。 当我使用它们时,如果它不适合容器,它们似乎打破了这个词。 但为什么W3C有两种方法呢?
#1楼
word-wrap: break-word
最近更改为overflow-wrap: break-word
- 将长单词包装到下一行。
- 调整不同的单词,使它们不会在中间突破。
word-break: break-all
- 无论是连续的单词还是多个单词,都要在宽度限制的边缘将它们分开。 (即使在同一个词的字符内)
因此,如果你有许多固定大小的跨度来动态获取内容,你可能只是喜欢使用word-wrap: break-word
,因为只有连续的单词在它们之间被打破,并且如果它是包含许多单词的句子,调整空格以获得完整的单词(单词中没有中断)。
如果没关系,那就去吧。
#2楼
这是我能找到的全部。 不确定它是否有帮助,但我想我会把它添加到混合中。
WORD-WRAP
此属性指定当内容超出元素的指定呈现框的边界时当前呈现的行是否应该中断(这在某些方面类似于intent中的''clip''和''overflow''属性。)此属性应仅适用如果元素具有可视化渲染,则是具有显式高度/宽度的内联元素,绝对定位和/或是块元素。
WORD-BREAK
此属性控制单词中的换行行为。 在元素中使用多种语言的情况下,它尤其有用。
#3楼
谈论这些的W3规范似乎暗示了word-break: break-all
用于要求CJK(中文,日文和韩文)文本的特定行为,而word-wrap: break-word
是更通用的,非-CJK意识到的行为。
#4楼
至少在Firefox(自v24起)和Chrome(截至第30版)中,当应用于table
元素中的内容时:
word-wrap:break-word
实际上不会导致长词包装,这可能导致表超出其容器的界限;
word-break:break-all
将导致单词换行,并且表格适合其容器。
jsfiddle演示 。
#5楼
使用分word-break
,一个很长的单词从它应该开始的点开始,并且只要需要它就会被破坏
[X] I am a text that 0123
4567890123456789012345678
90123456789 want to live
inside this narrow paragr
aph.
但是,对于word-wrap
,一个很长的单词不会从应该开始的那一刻开始。 它包裹到下一行,然后在需要时被打破
[X] I am a text that
012345678901234567890123
4567890123456789 want to
live inside this narrow
paragraph.
Jacob 模板替换生成 word 文件、word 合并、word 转 pdf 文件
Jacob 模板替换生成 word 文件、word 合并、word 转 pdf 文件
package com.sdp.utils;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.ComThread;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
import com.jfinal.kit.Prop;
import com.jfinal.kit.PropKit;
public class JacobWordUtil {
private static ActiveXComponent word = null;
private static Dispatch document = null;
private static Dispatch wordDoc = null;
/**
* WORD组件初始化
*/
public static void wordInit() {
System.out.println("ComThread.wordInit()");
ComThread.InitMTA(true);
word = new ActiveXComponent("Word.Application");
Dispatch.put(word, "Visible", new Variant(false));
word.setProperty("AutomationSecurity", new Variant(3)); // 禁用宏
document = word.getProperty("Documents").toDispatch();
System.out.println("ComThread.wordInitEND()");
}
/**
* 模板替换生成word文件
* @param templateName 模板名称(不带后缀)
* @param params 内容替换参数
* @param ispdf 是否转为PDF
* @return
* @throws Exception
*/
public static String executeWord(String templateName, List<?> params, boolean ispdf) throws Exception {
//初始化word
wordInit();
//模板文件路径
String tempPath = getTemplate(templateName);
//生产文件路径
String savePath = getFileStore(templateName);
//替换生成word
replace(params, tempPath, savePath, ispdf);
//关闭资源
release();
System.out.println("模板文件路径:" + tempPath);
System.out.println("生成文件路径:" + savePath);
return savePath;
}
/**
* 模板替换生成word文件
* @param filePath 待替换文件路径
* @param params 内容替换参数
* @param ispdf 是否转为PDF
* @return
* @throws Exception
*/
public static String executeWordPath(String filePath, List<?> params, boolean ispdf) throws Exception {
//初始化word
wordInit();
//生产文件路径
int index = filePath.lastIndexOf("/");
String savePath = filePath.substring(0, index) + "/word/" + filePath.substring(index, filePath.length());
//替换生成word
replace(params, filePath, savePath, ispdf);
//关闭资源
release();
System.out.println("文件路径:" + filePath);
System.out.println("生成文件路径:" + savePath);
return savePath;
}
/**
* 获取文件模板路径
*
* @return
* @throws IOException
*/
private static String getTemplate(String templateName) throws Exception {
String path = JacobWordUtil.class.getClassLoader().getResource("word-template").getPath();
String templatePath = path + templateName + ".doc";
return templatePath.substring(1, templatePath.length());
}
/**
* 获取文件存储路径
*
* @return
* @throws IOException
*/
private static String getFileStore(String templateName) throws IOException {
Prop propFile = PropKit.use("bs-fileserver.properties");
String fileRoot = propFile.get("bs.fileserver.local.root");
String fileStore = propFile.get("bs.fileserver.store");
//生成文件名称
String name = templateName + "_" + new SimpleDateFormat("yyyyMMddHHmmss").format(new Date());
return fileRoot + fileStore + "/" + templateName + "/" + name + ".doc";
}
/**
* 替换word中占位符
*/
@SuppressWarnings("unchecked")
private static void replace(List<?> params, String tempPath, String savePath, boolean ispdf) {
try {
// 不打开WORD复制
wordDoc = Dispatch.invoke(document,"Open",Dispatch.Method,
new Object[] { tempPath, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
// 查找替换内容
Dispatch selection = word.getProperty("Selection").toDispatch();
for (int i = 0; i < params.size(); i++) {
String findStr = "{" + (i + 1) + "}";
if (find(selection, findStr)) {
if(params.get(i) instanceof Map){
Map<String,Object> infoMap = (Map<String,Object>)params.get(i);
if(infoMap.get("img") != null){
String imagePath = infoMap.get("Path").toString();
Dispatch image = Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),
"AddPicture", imagePath).toDispatch();
//处理图片样式
setImgStyle(image, infoMap);
Dispatch.call(selection, "MoveRight");
}else {
//处理文字样式
setFont(selection, infoMap);
Dispatch.put(selection, "Text", infoMap.get("Text"));
}
}else if(params.get(i) instanceof String){
Dispatch.put(selection, "Text", params.get(i));
}
// 删除空行
if ("delete".equals(params.get(i))) {
Dispatch.put(selection, "Text", "");
Dispatch.call(selection, "Delete");
} else {
// 查询右移
Dispatch.call(selection, "MoveRight");
}
}
}
save(savePath, ispdf);// 保存
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 文件保存或另存为
*
* @param savePath 保存或另存为路径
*/
private static void save(String savePath, boolean ispdf) {
int index = savePath.lastIndexOf("/");
if(!new File(savePath.substring(0, index)).exists()){
new File(savePath.substring(0, index)).mkdirs();
}
if(ispdf){
Dispatch.put(wordDoc, "ShowRevisions", new Variant(false));
Dispatch.call((Dispatch) Dispatch.call(word, "WordBasic").getDispatch(),"FileSaveAs", savePath);
Dispatch.invoke(wordDoc, "SaveAs", Dispatch.Method,
new Object[] {savePath.replace(".doc", ".pdf"), new Variant(17)}, new int[1]);
}else{
Dispatch.call((Dispatch) Dispatch.call(word, "WordBasic").getDispatch(),"FileSaveAs", savePath);
}
}
private static boolean find(Dispatch selection, String toFindText) {
// 从selection所在位置开始查询
Dispatch find = Dispatch.call(selection, "Find").toDispatch();
// 设置要查找的内容
Dispatch.put(find, "Text", toFindText);
// 向前查找
Dispatch.put(find, "Forward", "True");
// 设置格式
Dispatch.put(find, "format", "True");
// 大小写匹配
Dispatch.put(find, "MatchCase", "True");
// 全字匹配
Dispatch.put(find, "MatchWholeWord", "True");
// 查找并选中
return Dispatch.call(find, "Execute").getBoolean();
}
/**
* 设置当前选定内容的字体
*
* @param image 文字对象
* @param param 文字参数
*/
private static void setFont(Dispatch selection, Map<String,Object> param) {
Dispatch font = Dispatch.get(selection, "Font").toDispatch();
//设置字体
if(param.get("Name") != null){
Dispatch.put(font, "Name", new Variant(param.get("Name")));
}
//设置粗体
Dispatch.put(font, "Bold", new Variant(param.get("Bold")!=null?param.get("Bold"):false));
//设置斜体
Dispatch.put(font, "Italic", new Variant(param.get("Italic")!=null?param.get("Bold"):false));
//设置下划线
Dispatch.put(font, "Underline", new Variant(param.get("Underline")!=null?param.get("Bold"):false));
//设置字体颜色
if(param.get("Color") != null){
Dispatch.put(font, "Color", Integer.valueOf(param.get("Color").toString(),16));
}
//设置字体大小
if(param.get("Size") != null){
Dispatch.put(font, "Size", param.get("Size"));
}
}
/**
* 设置图片样式
*
* @param image 图片对象
* @param param 图片参数
*
* 环绕格式(0-7)
* wdWrapSquare 0 使文字环绕形状。行在形状的另一侧延续。
* wdWrapTight 1 使文字紧密地环绕形状。
* wdWrapThrough 2 使文字环绕形状。
* wdWrapNone 3 将形状放在文字前面。
* wdWrapTopBottom 4 将文字放在形状的上方和下方。
* wdWrapBehind 5 将形状放在文字后面。
* wdWrapFront 6 将形状放在文字前面。
* wdWrapInline 7 将形状嵌入到文字中。
*/
private static void setImgStyle(Dispatch image, Map<String,Object> param){
//选中图片
Dispatch.call(image, "Select");
//图片的宽度
if(param.get("Width") != null){
Dispatch.put(image, "Width", new Variant(param.get("Width")));
}
//图片的高度
if(param.get("Height") != null){
Dispatch.put(image, "Height", new Variant(param.get("Height")));
}
//取得图片区域
Dispatch ShapeRange = Dispatch.call(image, "ConvertToShape").toDispatch();
//取得图片的格式对象
Dispatch WrapFormat = Dispatch.get(ShapeRange, "WrapFormat").toDispatch();
//设置环绕格式
Dispatch.put(WrapFormat, "Type", param.get("Type")!=null?param.get("Type"):6);
}
/**
* 关闭所有资源
*/
private static void release() {
// 始终释放资源
if(word != null){
word.invoke("Quit", new Variant[] {});
}
ComThread.Release();
}
/**
* word文件合并
* @param fileList 合并文件集合
* @param savepaths 合并后文件保存路径
*/
public static void uniteWord(List<String> fileList, String savepaths) throws Exception {
if (fileList.size() == 0 || fileList == null) {
return;
}
//打开word
ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word
// 设置word不可见
app.setProperty("Visible", new Variant(false));
//获得documents对象
Object docs = app.getProperty("Documents").toDispatch();
//打开第一个文件
Object doc = Dispatch.invoke((Dispatch) docs, "Open", Dispatch.Method,
new Object[] { (String) fileList.get(0), new Variant(false), new Variant(true) },
new int[3]).toDispatch();
//追加文件
for (int i = 1; i < fileList.size(); i++) {
Dispatch selection = Dispatch.get(app, "Selection").toDispatch();
// 到文档末尾
Dispatch.call(selection, "EndKey" , "6" );
//插入分页符
Dispatch.call(app, "Run", new Variant("InsertBreakWdSectionBreakNextPage"));
Dispatch.invoke(app.getProperty("Selection").toDispatch(),
"insertFile", Dispatch.Method, new Object[] {
(String) fileList.get(i), "",
new Variant(false), new Variant(false),
new Variant(false) }, new int[3]);
}
//保存新的word文件
Dispatch.invoke((Dispatch) doc, "SaveAs", Dispatch.Method,
new Object[] { savepaths, new Variant(1) }, new int[3]);
Variant f = new Variant(false);
Dispatch.call((Dispatch) doc, "Close", f);
}
/**
* word文件转PDF文件
* @param sfileName 待转word文件
* @param toFileName pdf保存路径
*/
public static void wordToPDF(String sfileName,String toFileName) throws Exception {
//初始化
wordInit();
//不打开WORD复制
wordDoc = Dispatch.invoke(document,"Open",Dispatch.Method,new Object[] {
sfileName, new Variant(false),new Variant(true) }, new int[1]).toDispatch();
File tofile = new File(toFileName);
if (tofile.exists()) {
tofile.delete();
}
Dispatch.call(word, "Run", new Variant("finalStatewordToPdf"));
Dispatch.invoke(wordDoc, "SaveAs", Dispatch.Method, new Object[] {
toFileName, new Variant(17) }, new int[1]);
//关闭资源
release();
}
public static void main(String[] args) {
//此处仅为调用说明与参数解释
//图片、带格式字符串参数如不需要的不传即可
try {
/**图片替换参数**/
Map<String,Object> map = new HashMap<String,Object>();
map.put("img", true);
map.put("Width", 30);
map.put("Height", 40);
map.put("Path", "D:\\wyh.png");
/**带格式字符替换参数**/
Map<String,Object> mapF = new HashMap<String,Object>();
mapF.put("Text", "地址:张川县竹园镇");
mapF.put("Name", "华文隶书"); //字体
mapF.put("Bold", true); //粗体
mapF.put("Italic", true); //斜体
mapF.put("Underline", true); //下划线
mapF.put("Color", "FFFFFF"); //颜色(例如白色:FFFFFF)
mapF.put("Size", 26); //字体大小
/**无格式字符替换参数**/
List<Object> params = Lists.newArrayList();
params.add("李四");
params.add("610321000000111111");
params.add(map);
params.add(mapF);
JacobWordUtil.executeWord("test", params, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
jacob 相关配置文件、word 下载:
链接:https://pan.baidu.com/s/1miR81Qg 密码:c3jw
LeetCode-1003 Check If Word Is Valid After Substitutions Solution (with Java)
1. Description:
Notes:
2. Examples:
3.Solutions:
1 /**
2 * Created by sheepcore on 2019-05-07
3 */
4 class Solution {
5 public boolean isValid(String s) {
6 Stack<Character> stack = new Stack<>();
7 for(char ch : s.toCharArray()){
8 switch(ch){
9 case ''a'':
10 case ''b'': stack.push(ch); break;
11 case ''c'':
12 if(!stack.isEmpty() && stack.peek() == ''b'')
13 stack.pop();
14 else
15 return false;
16 if(!stack.isEmpty() && stack.peek() == ''a'')
17 stack.pop();
18 else
19 return false;
20 break;
21 }
22 }
23 return stack.isEmpty();
24 }
25 }
今天的关于[Swift Weekly Contest 126]LeetCode1003. 检查替换后的词是否有效 | Check If Word Is Valid After Substitutions的分享已经结束,谢谢您的关注,如果想了解更多关于$word = new COM("word.application") or die("Unable to instantiate Word");、CSS中“word-break:break-all”与“word-wrap:break-word”之间的区别是什么?、Jacob 模板替换生成 word 文件、word 合并、word 转 pdf 文件、LeetCode-1003 Check If Word Is Valid After Substitutions Solution (with Java)的相关知识,请在本站进行查询。
本文标签: