G面经Prepare: Longest All One Substring

give a string, all 1 or 0, we can flip a 0 to 1, find the longest 1 substring after the flipping

这是一个简单版本of LC 424 Longest Repeating Character Replacement

又是Window, 又是Two Pointers

Window还是采用每次都try to update left使得window valid, 每次都检查最大window

 1 package GooglePhone;
 2
 3 public class LongestOneSubstr {
 4
 5     public static int find2(String str) {
 6         if (str==null || str.length()==0) return 0;
 7         int l = 0, r = 0;
 8         int maxLen = 0;
 9         int countZero = 0;
10         for (r=0; r<str.length(); r++) {
11             if (str.charAt(r) == ‘0‘) countZero++;
12             while (countZero > 1 && l < str.length()) {
13                 if (str.charAt(l) == ‘0‘) countZero--;
14                 l++;
15             }
16             maxLen = Math.max(r-l+1, maxLen);
17         }
18         return maxLen;
19     }
20
21     /**
22      * @param args
23      */
24     public static void main(String[] args) {
25         // TODO Auto-generated method stub
26         System.out.println(find2("10111000010110111101101010101"));
27     }
28
29 }
时间: 2024-10-19 09:43:25

G面经Prepare: Longest All One Substring的相关文章

LeetCode 1156. Swap For Longest Repeated Character Substring

原题链接在这里:https://leetcode.com/problems/swap-for-longest-repeated-character-substring/ 题目: Given a string text, we are allowed to swap two of the characters in the string. Find the length of the longest substring with repeated characters. Example 1: In

Longest Common Subsequence &amp; Substring &amp; prefix

Given two strings, find the longest common subsequence (LCS). Your code should return the length of LCS. Example For "ABCD" and "EDCA", the LCS is "A" (or "D", "C"), return 1. For "ABCD" and &quo

G面经prepare: Chucked Palindrome

给定一个字符串,找出最多有多少个chunked palindrome, 正常的palindrome是abccba, chunked palindrome的定义是:比如volvo, 可以把vo划分在一起,(vo) (l) (vo),那么它是个palindrome.求实现返回最大的chunk 数量. 比如aaaaaa可以是(aaa)(aaa), 但是最大chunk数量应该是(a)(a)(a)(a)(a)(a)为6 这就是一个greedy的问题,从string的两边开始,用i和j记录当前scan到的位

2Sigma OA prepare: Longest Chain

DP use HashMap: 根据string的长度sort,然后维护每个string的longest chain,default为1,如果删除某个char生成的string能提供更长的chain,则更新 1 package twoSigma; 2 3 import java.util.Arrays; 4 import java.util.Comparator; 5 import java.util.HashMap; 6 import java.lang.StringBuilder; 7 8

G面经Prepare: Print Zigzag Matrix

For instance, give row = 4, col = 5, print matrix in zigzag order like: [1, 8, 9, 16, 17] [2, 7, 10, 15, 18] [3, 6, 11, 14, 19] [4, 5, 12, 13, 20] 1 package GooglePhone; 2 3 import java.util.Arrays; 4 5 public class PrintMatrix { 6 7 static void prin

G面经prepare: Friends Recommendation

想想如果你用linkedin或者facebook, 给你一个人和他的朋友关系网,你会怎么给一个人推荐朋友 一个例子就是A-B, A-C, B - D, B - E, C - D,这个时候问我应该推荐谁给A,我说D,因为他是BC的共同好友,而E只是B的好友,到这我才明白干啥,就是给一个图和里面的一个节点A,用bfs从A出发,找出第二层中indegree度数最大节点 用HashMap<Character, HashSet<Character>>来建图 用HashMap<Chara

G面经prepare: Straight Partition of A Deck of Cards

Define “Straight” as 5 cards with consecutive numbers. Determine if the deck can be fully divided into sets of “Straight”. Example: 1, 2, 3, 4, 4, 5, 5, 6, 7, 8 -> True 这个是用一个hashtable,key是数字,value是出现次数 然后遍历原数组,每一个数字都把hash里从自己开始往后5个color数都-1,如果发现缺数则说

G面经Prepare: Search word delete sequence in dictionary

给一个单词一个字典,每次删除单词里任一个字母直到剩下一个字母,形成一个序列,比如office->offce->ofce->ofc->oc->c.问是否字典里存在一个这种序列 1 package checkDictExistSequence; 2 import java.util.*; 3 4 public class Solution { 5 HashSet<String> dict = new HashSet<String>(); 6 7 publi

G面经Prepare: Valid Preorder traversal serialized String

1 求问下各位大神,怎么判断一个按照Preorder traversal serialized的binary tree的序列是否正确呢?不能deserialize成树比如 2 A) 9 3 4 # # 1 # # 2 # 6 # #是对的,因为表示 3 9 4 / 5 3 2 6 / \ 7 4 1 6 8 B ) 9 3 4 # # 1 # #就是错的,因为无法反构造回一棵树 我觉得可以在字符串里找"n##"这种结构(对应tree里两个children都是Null的叶节点),找到之后