课程: Java Algorithms

今天就学习课程吧!

今天就开通帐号,24,700 门业界名师课程任您挑!

Apply custom parsing to search algorithms

Apply custom parsing to search algorithms - Java教程

课程: Java Algorithms

Apply custom parsing to search algorithms

- [Instructor] Sometimes you'll need to parse the string yourself in order to retrieve the data you're looking for. Let's say we wanted to find out if a certain character exists at an even index or an even location within the string. In this case, we'll need to access the location of the character as well as the character in the string itself. This means we'll use a for loop. Now, instead of incrementing, I by one, we could actually increment I by two. We only need to check the even indices of the string, since we're checking whether the item is at an even index, we can skip over the odd indices, cutting our time complexity in half. With this new incrementation, we'll need to change our conditions so we don't get a null pointer exception in the body of the for loop. We don't want to try to access a character at an invalid location. Since we're skipping every other indices in the string, we can change our condition to s.length divided by two plus one. Now let's add some code to the for…

内容