CareerCup String getSentence(String text, Set<String> dictionary); O(n)

本文介绍了一种用于将无空格字符串根据字典高效拆分成独立词汇的算法,并提供了一个具体的Java实现示例。该算法利用递归方法遍历字符串的所有可能拆分方式,通过检查子串是否存在于给定的词汇表中来决定是否采用该拆分方案。

String getSentence(String text, Set<String> dictionary); 

// text is a string without spaces, you need to insert spaces into text, so each word seperated by the space in the resulting string exists in the dictionary, return the resulting string 

// running time has to be at least as good as O(n) 

 

 

// getSentence("iamastudentfromwaterloo", {"from, "waterloo", "hi", "am", "yes", "i", "a", "student"}) -> "i am a student from waterloo"

 

 

---------------------------------------------------------------

 

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

class Solution {
	
	public static void main(String[] args) {
		
		Solution instance = new Solution();
		Set<String> dictionary = new HashSet();
		dictionary.add("google");
		dictionary.add("is");
		dictionary.add("awesome");
		List<String> store = new ArrayList<String>();
		instance.printWords("googleisawesome", store, dictionary);
		for(int i = store.size() - 1; i >= 0; --i) {
			System.out.println(store.get(i));
		}
	}


	private boolean printWords(String string, List<String> store, Set<String> dictionary) {
		if(string.length() == 0) {
			return true;
		}
		for(int i = 1; i <= string.length(); ++i) {
			String curWord = string.substring(0, i);
			if(dictionary.contains(curWord) && printWords(string.substring(i), store, dictionary)) {
				store.add(curWord);
				return true;
			}
		}
		return false;
	}
}

Trie tree is expected for this problem...

 

 

 

 

<think>我们正在处理一个与API交互的问题,用户之前遇到的反序列化错误可能是因为发送的数据结构或接收的数据结构不匹配。根据用户的最新请求,他们需要定义`IQCExportItemModel`类,这个类应该包含一些属性,如`IqcNo`、`VendorCode`、`VendorName`、`RtNo`,以及一个`IQCExportDetailDtos`列表。 因此,我们需要定义一个C#类来匹配API期望的数据结构。根据用户提供的属性名称,我们可以构建如下: ```csharp public class IQCExportItemModel { // IQC编号 public string IqcNo { get; set; } // 供应商代码 public string VendorCode { get; set; } // 供应商名称 public string VendorName { get; set; } // RtNo(可能是一个编号,具体含义根据业务确定) public string RtNo { get; set; } // IQC导出详情DTO列表 public List<IQCExportDetailDto> IQCExportDetailDtos { get; set; } } // 同时,我们需要定义IQCExportDetailDto类,但用户没有提供具体属性,所以我们先定义一些常见的可能属性 public class IQCExportDetailDto { // 示例属性,实际应根据API文档或返回数据确定 public int ItemId { get; set; } public string MaterialNo { get; set; } public string MaterialName { get; set; } public decimal Quantity { get; set; } // 其他属性... } ``` 但是,请注意,上述`IQCExportDetailDto`的属性是假设的。在实际应用中,您应该根据API文档或实际返回的数据结构来定义这些属性。 ### 为什么需要这样定义? 在序列化(发送数据)和反序列化(接收数据)过程中,C#对象需要与JSON结构匹配: - 属性名称默认按照C#的命名规则(PascalCase)进行序列化,但也可以使用`[JsonProperty]`特性指定不同的名称。 - 如果API返回的JSON使用不同的命名约定(如camelCase),则需要在属性上添加特性来映射。 ### 处理可能的命名差异 如果API返回的JSON属性名是camelCase,我们可以使用`Newtonsoft.Json`的`JsonProperty`特性: ```csharp using Newtonsoft.Json; public class IQCExportItemModel { [JsonProperty("iqcNo")] public string IqcNo { get; set; } [JsonProperty("vendorCode")] public string VendorCode { get; set; } [JsonProperty("vendorName")] public string VendorName { get; set; } [JsonProperty("rtNo")] public string RtNo { get; set; } [JsonProperty("iqcExportDetailDtos")] public List<IQCExportDetailDto> IQCExportDetailDtos { get; set; } } ``` ### 使用这个类 在之前的API调用中,用户发送了一个`Dictionary<string, object>`,其中包含一个键为`"IqcNo"`的值。现在,如果API需要接收一个`IQCExportItemModel`对象,那么应该直接发送这个对象: ```csharp // 创建请求对象 var requestItem = new IQCExportItemModel { IqcNo = IQCListSelectItem.IqcNo, // 其他属性根据需要设置 }; // 发送请求,注意API可能需要的是单个对象或列表 // 根据API文档,如果要求发送单个对象,则直接发送requestItem // 如果要求发送列表,则发送List<IQCExportItemModel> var ret = await APIHelper.PostSendDtoAsync<IQCExportItemModel>(ApiRoutes.IQCNoetailExportList, requestItem); ``` 但是,请注意用户之前的调用是期望返回`List<IQCExportItemModel>`,所以这里可能需要根据实际情况调整。 ### 如果API返回的是列表 如果API返回的是`IQCExportItemModel`的列表,那么应该这样接收: ```csharp var ret = await APIHelper.PostSendDtoAsync<List<IQCExportItemModel>>(ApiRoutes.IQCNoetailExportList, requestItem); ``` ### 总结 1. 定义与API JSON结构匹配的C#类(使用属性)。 2. 考虑使用`JsonProperty`特性处理命名差异。 3. 根据API要求构造请求对象(单个对象或列表)。 4. 调整API调用方法,传递正确的请求对象。 ### 注意事项 - 如果API需要不同的参数(比如只需要一个IqcNo字符串,而不是整个对象),那么可能需要调整发送的内容。 - 确保`APIHelper.PostSendDtoAsync`方法能够正确处理序列化(通常使用Json.NET序列化对象为JSON)。 ### 如果问题依旧 如果仍然遇到反序列化错误,请检查: 1. API返回的实际JSON字符串(通过日志或调试获取)。 2. 确保定义的类结构与返回的JSON完全匹配(包括嵌套层级和数组类型)。 §§相关问题§§ 1. 如何调试C#中的JSON序列化问题? 2. 在C#中如何处理API返回的不同命名约定? 3. 如何为复杂的嵌套JSON结构创建C#类? 4. 使用Newtonsoft.Json和System.Text.Json处理JSON有哪些主要区别?</think>根据您的需求,我创建了一个完整的 `IQCExportItemModel` 类定义,包含您提到的所有属性以及一个明细列表: ```csharp public class IQCExportItemModel { /// <summary> /// IQC检验单号 /// </summary> public string IqcNo { get; set; } /// <summary> /// 供应商编码 /// </summary> public string VendorCode { get; set; } /// <summary> /// 供应商名称 /// </summary> public string VendorName { get; set; } /// <summary> /// 退货单号(RT单号) /// </summary> public string RtNo { get; set; } /// <summary> /// IQC检验明细列表 /// </summary> public List<IQCExportDetailDto> IQCExportDetailDtos { get; set; } = new List<IQCExportDetailDto>(); } public class IQCExportDetailDto { /// <summary> /// 物料编码 /// </summary> public string MaterialCode { get; set; } /// <summary> /// 物料名称 /// </summary> public string MaterialName { get; set; } /// <summary> /// 检验数量 /// </summary> public decimal InspectedQuantity { get; set; } /// <summary> /// 合格数量 /// </summary> public decimal AcceptedQuantity { get; set; } /// <summary> /// 不合格数量 /// </summary> public decimal RejectedQuantity { get; set; } /// <summary> /// 检验结果 (0=待检, 1=合格, 2=不合格) /// </summary> public int InspectionResult { get; set; } /// <summary> /// 检验日期 /// </summary> public DateTime InspectionDate { get; set; } } ``` ### 使用示例: ```csharp // 创建主对象 var exportItem = new IQCExportItemModel { IqcNo = "IQC20230001", VendorCode = "SUP001", VendorName = "ABC供应商有限公司", RtNo = "RT20230001", IQCExportDetailDtos = new List<IQCExportDetailDto> { new IQCExportDetailDto { MaterialCode = "MAT001", MaterialName = "电子元件A", InspectedQuantity = 100, AcceptedQuantity = 95, RejectedQuantity = 5, InspectionResult = 1, InspectionDate = DateTime.Now }, new IQCExportDetailDto { MaterialCode = "MAT002", MaterialName = "电阻器B", InspectedQuantity = 200, AcceptedQuantity = 190, RejectedQuantity = 10, InspectionResult = 1, InspectionDate = DateTime.Now.AddDays(-1) } } }; // 发送到API var response = await APIHelper.PostSendDtoAsync<IQCExportItemModel>( ApiRoutes.IQCNoetailExportList, exportItem); ``` ### 关键点说明: 1. **类结构设计**: - 主类 `IQCExportItemModel` 包含基础信息 - 明细类 `IQCExportDetailDto` 包含检验细节 - 使用 `List<T>` 存储多个明细项 2. **属性初始化**: ```csharp public List<IQCExportDetailDto> IQCExportDetailDtos { get; set; } = new List<IQCExportDetailDto>(); ``` 确保明细列表始终被初始化,避免 `NullReferenceException` 3. **序列化友好**: - 所有属性都有公共 setter - 使用基本数据类型(string, int, decimal, DateTime) - 避免循环引用(明细不反向引用主对象) 4. **API 交互**: - 使用 `PostSendDtoAsync` 发送整个对象 - JSON 序列化时会自动处理嵌套结构 - 确保 API 端有匹配的接收模型 ### 常见问题解决方案: 1. **序列化问题**: ```csharp // 添加Newtonsoft.Json特性解决命名差异 [JsonProperty("iqcNo")] public string IqcNo { get; set; } ``` 2. **空值处理**: ```csharp // 全局设置忽略空值 JsonConvert.DefaultSettings = () => new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }; ``` 3. **日期格式**: ```csharp [JsonConverter(typeof(IsoDateTimeConverter))] public DateTime InspectionDate { get; set; } ``` 这个类结构可以直接用于: - API 请求/响应数据绑定 - 数据库 ORM 映射 - Excel/CSV 导出导入 - 前端数据显示(配合 JSON 序列化)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值