implement strstr java_LeetCode 28. Implement strStr()

题目:

Implement strStr().

Return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Example 1:

Input: haystack = "hello", needle = "ll"

Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"

Output: -1

Clarification:

What should we return when needle is an empty string? This is a great question to ask during an interview.

For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().

题解:

Brute force的算法是从haystack头开始走,每次取后面长度与needle长度相等的substring, 看这个substring与needle是否相同即可.

If needle is empty, return 0.

Note: s.substring(i), i could be s.length(), and return "". Thus here, it is allowed to use i <= m - n.

Time Complexity: O(m*n), m = haystack.length(), n = needle.length().

Space: O(1).

AC Java:

1 classSolution {2 public intstrStr(String haystack, String needle) {3 if(haystack == null || needle == null){4 return -1;5 }6

7 if(needle.length() == 0){8 return 0;9 }10

11 int m =haystack.length();12 int n =needle.length();13

14 for(int i = 0; i <= m - n; i++){15 if(haystack.substring(i, i +n).equals(needle)){16 returni;17 }18 }19

20 return -1;21 }22 }

本题还可以采用KMP算法,这篇帖子说的很详细。

注意在求next数组时,while loop的条件是q

Time Complexity: O(m+n). m = haystack.length(), n = needle.length(). Space: O(n)

AC Java:

1 public classSolution {2 public intstrStr(String haystack, String needle) {3 if(haystack == null || needle == null || haystack.length()

7 if(needle.length() == 0){8 return 0;9 }10

11 int i = 0;12 int j = 0;13 int len1 =haystack.length();14 int len2 =needle.length();15 int [] next = new int[len2];16 getNext(needle, next);17 while(i

26 if(j ==len2){27 return i-j;28 }else{29 return -1;30 }31 }32

33 private void getNext(String s, int[] next){34 int len =s.length();35 next[0] = -1;36 int p = -1;37 int q = 0;38 while(q < len-1){39 //s.charAt(p)表示前缀. s.charAt(q)表示后缀

40 if(p == -1 || s.charAt(p) ==s.charAt(q)){41 p++;42 q++;43 next[q] =p;44 }else{45 p =next[p];46 }47 }48 }49 }

参见如下两篇文章:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 这个错误通常是因为尝试将符号化的Keras输入/输出传递给NumPy调用,而这是不支持的。或者,可能尝试将Keras符号化的输入/输出传递给TF API,但该API没有注册分派,从而阻止Keras自动将API调用转换为函数式模型中的lambda层。\[1\]要解决这个问题,可以使用函数式API建模的形式,例如使用keras.Model(inputs=...)来定义模型。\[2\]另外,如果遇到Keras符号化输入/输出没有实现`__len__`的问题,可能是因为输出节点不在图中。可以尝试使用convert_variables_to_constants函数来指定保存的节点名称,而不是张量的名称。确保输出节点名称正确,并检查是否是由于TensorFlow版本不一致导致的问题。\[3\] #### 引用[.reference_title] - *1* [TypeError: Cannot convert a symbolic Keras input/output to a numpy array.](https://blog.csdn.net/weixin_43333607/article/details/129401065)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [用 Keras/TensorFlow 2.9 创建深度学习模型的方法总结](https://blog.csdn.net/drin201312/article/details/125098197)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [keras训练的h5模型转换为pb模型](https://blog.csdn.net/dycljj/article/details/118304855)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^insertT0,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值