遍历字符串,获取字符串长度,判断字符串是否为纯数字,字符串转数字,求解《2496. 数组中字符串的最大值》和《657. 机器人能否返回原点》

2023-06-23 13:17:42
遍历字符串,获取字符串长度,判断字符串是否为纯数字,字符串转数字,求解《2496. 数组中字符串的最大值》和《657. 机器人能否返回原点》

字符串转数字

+"str"
num, _ := strconv.Atoi("str")
+$str
Integer.parseInt("str")
int.Parse("str")
stoi("str")
atoi("str")
int("str")

获取字符串长度(数值)

str.length
len(str)
strlen($str)
str.length()
str.Length
(int)str.size()
strlen(str)
len(str)

遍历字符串中每一个字符

for (const c of str) {}
for _, c := range str {}
$n = strlen($str);
for ($i = 0; $i < $n; $i++){}
int n = str.length();
for (int i = 0; i < n; i++){}
foreach (char c in str) {}
for (char& c : str) {}
int n = strlen(str);
for (int i = 0; i < n; i++) {}
for (c in str) {}

例题

2496. 数组中字符串的最大值

一个由字母和数字组成的字符串的 值 定义如下:
如果字符串 只 包含数字,那么值为该字符串在 10 进制下的所表示的数字。
否则,值为字符串的 长度 。
给你一个字符串数组 strs ,每个字符串都只由字母和数字组成,请你返回 strs 中字符串的 最大值 。
示例 1:
输入:strs = ["alic3","bob","3","4","00000"]
输出:5
解释:

  • "alic3" 包含字母和数字,所以值为长度 5 。
  • "bob" 只包含字母,所以值为长度 3 。
  • "3" 只包含数字,所以值为 3 。
  • "4" 只包含数字,所以值为 4 。
  • "00000" 只包含数字,所以值为 0 。
    所以最大的值为 5 ,是字符串 "alic3" 的值。

答案

顺序遍历

var maximumValue = function(strs) { // 正则
  const n = strs.length
  let ans = 0
  for (let i = 0; i < n; i++) {
    ans = Math.max(ans, /^\d+$/.test(strs[i]) ? +strs[i] : strs[i].length)
  }
  return ans
};
var maximumValue = function(strs) {
  let ans = 0
  for (const str of strs) {
    let isDigit = true
    for (const c of str) {
      isDigit &= c >= '0' && c <= '9'
    }
    ans = Math.max(ans, isDigit ? +str : str.length)
  }
  return ans
};
func maximumValue(strs []string) int {
  ans := 0
  for _, str := range strs {
    isDigit := true
    for _, c := range str {
      isDigit = isDigit && c >= '0' && c <= '9'
    }
    if isDigit == true {
      num, _ := strconv.Atoi(str)
      ans = max(ans, num)
    } else {
      ans = max(ans, len(str))
    }
  }
  return ans
}
func max(a, b int) int {
  if a > b {
    return a
  }
  return b
}
class Solution {
  function maximumValue($strs) {
    $ans = 0;
    foreach($strs as $str) {
      $n = strlen($str);
      $isDigit = true;
      for ($i = 0; $i < $n; $i++) {
        $c = $str[$i];
        $isDigit &= $c >= '0' && $c <= '9';
      }
      $ans = max($ans, $isDigit ? +$str : strlen($str));
    }
    return $ans;
  }
}
class Solution {
  public int maximumValue(String[] strs) {
    int ans = 0;
    for (String str : strs) {
      boolean isDigit = true;
      int n = str.length();
      for (int i = 0; i < n; i++) {
        isDigit &= Character.isDigit(str.charAt(i));
      }
      ans = Math.max(ans, isDigit ? Integer.parseInt(str) : str.length());
    }
    return ans;
  }
}
public class Solution {
  public int MaximumValue(string[] strs) {
    int ans = 0;
    foreach (string str in strs) {
      bool isDigit = true;
      foreach (char c in str) {
        isDigit &= char.IsDigit(c);
      }
      ans = Math.Max(ans, isDigit ? int.Parse(str) : str.Length);
    }
    return ans;
  }
}
class Solution {
public:
  int maximumValue(vector<string>& strs) {
    int ans = 0;
    for (string& str : strs) {
      bool isDigit = true;
      for (char& c : str) {
        isDigit &= isdigit(c);
      }
      ans = max(ans, isDigit ? stoi(str) : (int)str.size());
    }
    return ans;      
  }
};
#define MAX(a, b) (a > b ? a : b)
int maximumValue(char ** strs, int strsSize){
  int ans = 0;
  for (int i = 0; i < strsSize; i++) {
    char* str = strs[i];
    int n = strlen(str);
    bool isDigit = true;
    for (int j = 0; j < n; j++) {
      char c = str[j];
      isDigit &= c >= '0' && c <= '9';
    }
    ans = MAX(ans, (isDigit == true ? atoi(str) : n));
  }
  return ans;
}
class Solution:
  def maximumValue(self, strs: List[str]) -> int:
    ans = 0
    for s in strs:
      ans = max(ans, int(s) if s.isdigit() == True else len(s))
    return ans

657. 机器人能否返回原点

在二维平面上,有一个机器人从原点 (0, 0) 开始。给出它的移动顺序,判断这个机器人在完成移动后是否在 (0, 0) 处结束。
移动顺序由字符串 moves 表示。字符 move[i] 表示其第 i 次移动。机器人的有效动作有 R(右),L(左),U(上)和 D(下)。
如果机器人在完成所有动作后返回原点,则返回 true。否则,返回 false。
注意:机器人“面朝”的方向无关紧要。 “R” 将始终使机器人向右移动一次,“L” 将始终向左移动等。此外,假设每次移动机器人的移动幅度相同。
示例 1:
输入: moves = "UD"
输出: true
解释:机器人向上移动一次,然后向下移动一次。所有动作都具有相同的幅度,因此它最终回到它开始的原点。因此,我们返回 true。

答案

顺序遍历

var judgeCircle = function(moves) {
  let x = 0, y = 0
  for (const move of moves) {
    if (move === 'U') x++;
    else if (move === 'D') x--;
    else if (move === 'L') y++;
    else y--; 
  }
  return x === 0 && y === 0
};
func judgeCircle(moves string) bool {
  x, y := 0, 0
  for _, move := range moves {
    if move == 'U' {
      x++;
    } else if move == 'D' {
      x--;
    } else if move == 'L' {
      y++;
    } else {
      y--; 
    }
  }
  return x == 0 && y == 0
}
class Solution {
  function judgeCircle($moves) {
    $x = 0;
    $y = 0;
    $n = strlen($moves);
    for ($i = 0; $i < $n; $i++) {
      if ($moves[$i] === 'R') $x++;
      elseif ($moves[$i] === 'L') $x--;
      elseif ($moves[$i] === 'U') $y++;
      else $y--;
    }
    return $x === 0 && $y === 0;
  }
}
class Solution {
  public boolean judgeCircle(String moves) {
    int x = 0, y = 0, n = moves.length();
    for (int i = 0; i < n; i++) {
      if (moves.charAt(i) == 'R') x++;
      else if (moves.charAt(i) == 'L') x--;
      else if (moves.charAt(i) == 'U') y++;
      else y--;
    }
    return x == 0 && y == 0;
  }
}
public class Solution {
  public bool JudgeCircle(string moves) {
    int x = 0, y = 0, n = moves.Length;
    for (int i = 0; i < n; i++) {
      if (moves[i] == 'R') x++;
      else if (moves[i] == 'L') x--;
      else if (moves[i] == 'U') y++;
      else y--;
    }
    return x == 0 && y == 0;
  }
}
class Solution {
public:
  bool judgeCircle(string moves) {
    int x = 0, y = 0;
    for (const char move : moves) {
      if (move == 'R') x++;
      else if (move == 'L') x--;
      else if (move == 'U') y++;
      else y--;
    }
    return x == 0 && y == 0;
  }
};
bool judgeCircle(char * moves){
  int x = 0, y = 0, n = strlen(moves);
  for (int i = 0; i < n; i++) {
    if (moves[i] == 'R') x++;
    else if (moves[i] == 'L') x--;
    else if (moves[i] == 'U') y++;
    else y--;
  }
  return x == 0 && y == 0;
}
class Solution:
  def judgeCircle(self, moves: str) -> bool:
    x, y = 0, 0
    for move in moves:
      if move == 'R': x += 1
      elif move == 'L': x -= 1
      elif move == 'U': y += 1
      else: y -= 1
    return x == 0 and y == 0

字符串的 append 操作,矩阵中两点曼哈顿距离最短,求解《1138. 字母板上的路径》
字符串的 append 操作,矩阵中两点曼哈顿距离最短,求解《1138. 字母板上的路径》
栈、顺序遍历,为可变数组添加元素,2 解法求解《1441. 用栈操作构建数组》
栈、顺序遍历,为可变数组添加元素,2 解法求解《1441. 用栈操作构建数组》
顺序遍历,哈希集合,求解《817. 链表组件》
顺序遍历,哈希集合,求解《817. 链表组件》