substring

Codeforces Round #617 (Div. 3) C.Yet Another Walking Robot

南笙酒味 提交于 2020-02-06 11:25:28
Codeforces Round #617 (Div. 3) C.Yet Another Walking Robot There is a robot on a coordinate plane. Initially, the robot is located at the point (0,0). Its path is described as a string s of length n consisting of characters ‘L’, ‘R’, ‘U’, ‘D’. Each of these characters corresponds to some move: ‘L’ (left): means that the robot moves from the point (x,y) to the point (x−1,y); ‘R’ (right): means that the robot moves from the point (x,y) to the point (x+1,y); ‘U’ (up): means that the robot moves from the point (x,y) to the point (x,y+1); ‘D’ (down): means that the robot moves from the point (x,y)

C. Yet Another Walking Robot Round #617 (Div. 3)()(map + 前后相同状态的存储)

不想你离开。 提交于 2020-02-05 22:51:37
C. Yet Another Walking Robot time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output There is a robot on a coordinate plane. Initially, the robot is located at the point (0,0)(0,0). Its path is described as a string 𝑠s of length 𝑛n consisting of characters ‘L’, ‘R’, ‘U’, ‘D’. Each of these characters corresponds to some move: ‘L’ (left): means that the robot moves from the point (𝑥,𝑦)(x,y) to the point (𝑥−1,𝑦)(x−1,y); ‘R’ (right): means that the robot moves from the point (𝑥,𝑦)(x,y) to the point (𝑥+1,𝑦)(x+1,y); ‘U’ (up): means that the robot

3. Longest Substring Without Repeating Characters

天大地大妈咪最大 提交于 2020-02-05 11:52:30
Given a string, find the length of the longest substring without repeating characters. Example 1: Input: "abcabcbb" Output: 3 Explanation: The answer is "abc", with the length of 3. Example 2: Input: "bbbbb" Output: 1 Explanation: The answer is "b", with the length of 1. Example 3: Input: "pwwkew" Output: 3 Explanation: The answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring. 给定一个字符串,求出它最长的没有重复字符的子串的长度。法一:定义字符串str、rst,遍历给定的字符串s,每遍历一个字符c,判断它是否在字符串str中存在,若不存在,直接添加到字符串str末尾,若存在,比较字符串str与rst的长度,若前者比后者长,将字符串赋给rst

substring,substr,和slice的区别

别说谁变了你拦得住时间么 提交于 2020-02-05 04:35:40
1.Substring(x,y) : 输出一个字符串,当其中只有一个参数时,会输出从x开始到结尾的String。 举例: var str = "javaScript"; console.log( str .substring(1)); 输出结果为: avaScript 如果有两个参数,则会输出从x到y的值,值得注意的时候,这里的x ,y可以理解成一个 (x, y]的区间,即不包含第x个元素,但包含第y个元素, x,y均从1开始计算 举例: var str="javaScript"; console.log(str.substring(3,5)); 输出结果为 :aS。 另外当x<y的情况时,系统会自动调整x,y的位置并输出也就是说 var str="javaScript"; console.log(str.substring(5,3)); 这俩个结果是一样的。 如y为负值,则直接输出为x之前的字符串 举例 var str="javaScript"; console.log(str.substring(3,-5)); 结果为: jav; 2.Substr(x,y): 和substring不同,substr内的x,y属性分别代表元素的起始位置,及输出的元素长度。举例: var str="javaScript"; console.log(str.substr(3,5)); 输出结果为

JS substring substr slice区别

谁说胖子不能爱 提交于 2020-02-05 04:28:52
1、api说明 (1) substring str.substring(indexStart[, indexEnd]) substring 提取从 indexStart 到 indexEnd ( 不包括 )之间的字符。特别地: 如果 indexStart 等于 indexEnd , substring 返回一个空字符串。 如果省略 indexEnd , substring 提取字符一直到字符串末尾。 如果任一参数小于 0 或为 NaN ,则被当作 0。 如果任一参数大于 stringName.length ,则被当作 stringName.length 。 如果 indexStart 大于 indexEnd ,则 substring 的执行效果就像两个参数调换了一样。 说明:参数不支持 负数 ! (2) substr str.substr(start[, length]) tart 是一个字符的索引。首字符的索引为 0,最后一个字符的索引为 字符串的长度减去1。 substr 从 start 位置开始提取字符,提取 length 个字符(或直到字符串的末尾)。 如果 start 为正值,且大于或等于字符串的长度,则 substr 返回一个空字符串。 如果 start 为负值,则 substr 把它作为从字符串末尾开始的一个字符索引。如果 start 为负值且 abs(start)

slice,substr和substring的区别

亡梦爱人 提交于 2020-02-05 04:28:41
首先,他们都接收两个参数,slice和substring接收的是起始位置和结束位置(不包括结束位置),而substr接收的则是起始位置和所要返回的 字符串长度 。直接看下面例子: 1 var test = 'hello world';2 3 alert(test.slice(4,7)); //o w4 alert(test.substring(4,7)); //o w5 alert(test.substr(4,7)); //o world 这里有个需要注意的地方就是:substring是以两个参数中较小一个作为起始位置,较大的参数作为结束位置。 如: alert(test.substring(7,4)); //o w 接着,当接收的参数是负数时,slice会将它字符串的长度与对应的负数相加,结果作为参数;substr则仅仅是将第一个参数与字符串长度相加后的结果作为第一个参数;substring则干脆将负参数都直接转换为0。测试代码如下: 1 var test = 'hello world';2 3 alert(test.slice(-3)); //rld4 alert(test.substring(-3)); //hello world5 alert(test.substr(-3)); //rld6 alert(test.slice(3,-4)); //lo w7 alert

Longest Palindrome Substring

蹲街弑〆低调 提交于 2020-02-04 18:31:50
Given a string s , find the longest palindromic substring in s . You may assume that the maximum length of s is 1000. Example 1: Input: "babad" Output: "bab" Note: "aba" is also a valid answer. Example 2: Input: "cbbd" Output: "bb" 思路:有中心生长法,就是判断所有的palindrome,比较长度之后,返回最大值,palindrome分odd和even class Solution { public String longestPalindrome(String s) { if(s == null || s.length() == 0) { return s; } String maxstr = ""; for(int i = 0; i < s.length(); i++) { String oddstr = findPalindrome(s, i, i); if(oddstr.length() > maxstr.length()){ maxstr = oddstr; } } for(int i = 0; i < s.length(); i++) {

3. Longest Substring Without Repeating Characters

生来就可爱ヽ(ⅴ<●) 提交于 2020-02-04 11:24:03
思路: begin (最重要的变量)记录longest substring开始的位置;longest当前substring的长度;map存放字符的index。实质上是维护了一个substring区间,使得substring内的所有字符都是独一无二的。 class Solution { public : int lengthOfLongestSubstring ( string s ) { if ( s . length ( ) <= 1 ) { return s . length ( ) ; } unordered_map < char , int > map ; int ans = 0 ; int longest = 0 ; int begin = 0 ; for ( int i = 0 ; i < s . length ( ) ; ++ i ) { if ( map . count ( s [ i ] ) == 0 || map [ s [ i ] ] < begin ) { // 只要map中的字符index不在begin之后,都视为不存在 map [ s [ i ] ] = i ; ++ longest ; } else { int before = map [ s [ i ] ] ; map [ s [ i ] ] = i ; longest = i - before ;

Group strings by longest common starting substring

佐手、 提交于 2020-02-02 12:51:16
问题 Here's the problem. Say I have these strings: apple ipad mini 32gb apple ipad mini 64gb apple ipad air 64gb apple ipad air 32gb panasonic gh4 samsung s2 galaxy samsung s2 galaxy red samsung s3 galaxy I want these strings to be grouped like this: apple ipad mini: [apple ipad mini 32gb, apple ipad mini 64gb] apple ipad air: [apple ipad air 64gb, apple ipad 32gb] panasonic gh4: [panasonic gh4] samsung s2 galaxy: [samsung s2 galaxy, samsung s2 galaxy red] samsung s3 galaxy The point is to

Finding out whether there exist two identical substrings one next to another

我的梦境 提交于 2020-02-01 17:50:11
问题 We've got a String. ABAEABABEABE Now we've got to check whether there exist a substring that is followed next by another substring that's exactly the same as the first one. In this example: ABAEAB ABE ABE ABE is followed by ABE and that are two identical substrings. In this example: AAB It would be simply A, beacuse A is followed by another A. In this example: ABCDEFGHIJKLMNO There doesn't exist such a substring, so the answer would be NO. I only managed to find an algorithm that would run in