原生 API 和顺序遍历,检查字符串包含另一字符串,2 解法求解《1784. 检查二进制字符串字段》

2022-10-03 01:23:58
原生 API 和顺序遍历,用 indexOf / includes / strpos / strstr / strings.Contains / ().contains / ().Contains / ().find == string::npos / (not) in 检查字符串是否包含另一字符串,2 解法求解《1784. 检查二进制字符串字段》

例题

1784. 检查二进制字符串字段

给你一个二进制字符串 s ,该字符串 不含前导零 。
如果 s 包含 零个或一个由连续的 '1' 组成的字段 ,返回 true​​​ 。否则,返回 false 。
如果 s 中 由连续若干个 '1' 组成的字段 数量不超过 1,返回 true​​​ 。否则,返回 false 。
示例 1: 输入:s = "1001" 输出:false
解释:由连续若干个 '1' 组成的字段数量为 2,返回 false
示例 2:
输入:s = "110"
输出:true
提示:
1 <= s.length <= 100
s[i]​​​​ 为 '0' 或 '1'
s[0] 为 '1'

答案

顺序遍历

var checkOnesSegment = function(s) {
  const n = s.length
  for (let i = 2; i < n; i++) {
    if (s[i - 1] === '0' && s[i] === '1') return false
  }
  return true
};
function checkOnesSegment(s: string): boolean {
  const n = s.length
  for (let i = 2; i < n; i++) {
    if (s[i - 1] === '0' && s[i] === '1') return false
  }
  return true
};
class Solution {
  function checkOnesSegment($s) {
    $n = strlen($s);
    for ($i = 2; $i < $n; $i++) {
      if ($s[$i - 1] === '0' && $s[$i] === '1') return false;
    }
    return true;
  }
}
func checkOnesSegment(s string) bool {
  n := len(s)
  for i := 2; i < n; i++ {
    if s[i - 1] == '0' && s[i] == '1' {
      return false
    }
  }
  return true
}
class Solution {
  public boolean checkOnesSegment(String s) {
    int n = s.length();
    for (int i = 2; i < n; i++) {
      if (s.charAt(i - 1) == '0' && s.charAt(i) == '1') return false;
    }
    return true;
  }
}
public class Solution {
  public bool CheckOnesSegment(string s) {
    int n = s.Length;
    for (int i = 2; i < n; i++) {
      if (s[i - 1] == '0' && s[i] == '1') return false;
    }
    return true;
  }
}
bool checkOnesSegment(char * s){
  int n = strlen(s);
  for (int i = 2; i < n; i++) {
    if (s[i - 1] == '0' && s[i] == '1') return false;
  }
  return true;
}
class Solution {
public:
  bool checkOnesSegment(string s) {
    int n = s.size();
    for (int i = 2; i < n; i++) {
      if (s[i - 1] == '0' && s[i] == '1') return false;
    }
    return true;
  }
};
class Solution:
  def checkOnesSegment(self, s: str) -> bool:
    for i in range(2, len(s)):
      if s[i - 1] == '0' and s[i] == '1': return False
    return True

原生 API

var checkOnesSegment = function(s) {
  return s.indexOf('01') === -1
};
function checkOnesSegment(s: string): boolean {
  return s.includes('01') === false
};
class Solution {
  function checkOnesSegment($s) {
    return strpos($s, '01') === false;
  }
}
class Solution {
  function checkOnesSegment($s) {
    return strstr($s, '01') === false;
  }
}
func checkOnesSegment(s string) bool {
  return strings.Contains(s, "01") == false
}
class Solution {
  public boolean checkOnesSegment(String s) {
    return s.contains("01") == false;
  }
}
public class Solution {
  public bool CheckOnesSegment(string s) {
    return s.Contains("01") == false;
  }
}
bool checkOnesSegment(char * s){
  return strstr(s, "01") == false;
}
class Solution {
public:
  bool checkOnesSegment(string s) {
    return s.find("01") == string::npos;
  }
};
class Solution:
  def checkOnesSegment(self, s: str) -> bool:
    return '01' not in s

栈、顺序遍历,为可变数组添加元素,2 解法求解《1441. 用栈操作构建数组》
栈、顺序遍历,为可变数组添加元素,2 解法求解《1441. 用栈操作构建数组》
顺序遍历,哈希集合,求解《817. 链表组件》
顺序遍历,哈希集合,求解《817. 链表组件》
顺序遍历,求解《1790. 仅执行一次字符串交换能否使两个字符串相等》
顺序遍历,求解《1790. 仅执行一次字符串交换能否使两个字符串相等》