2020-08-10

387. 字符串中的第一个唯一字符
剑指 Offer 50. 第一个只出现一次的字符

给定一个字符串,找到它的第一个不重复的字符,并返回它的索引。如果不存在,则返回 -1。
示例:
s = “leetcode” 返回 0
s = “loveleetcode” 返回 2

  func firstUniqChar(_ s: String) -> Character {
         var table: [Character: Int] = [:]
      
        for c in s {
           table[c, default:0] += 1
        }
        
        for (index, c) in s.enumerated() {
            if table[c] == 1 {
                return c
            }
        }
        return " ";
    }

8. 字符串转换整数 (atoi)

public int myAtoi(String str) {

	  public int myAtoi(String str) {
        // 判断有效性
        if (str.isEmpty()) return 0;
        // 转换值
        int base = 0;
        // 正负号
        int sign = 1;
        // 索引
        int i = 0;
        // 判断空格
        while (i < str.length() &&  str.charAt(i) == ' ') {
            i++;
        }
        // 判断正负号
        if (i < str.length() && (str.charAt(i) == '+' || str.charAt(i) == '-')) {
            sign = str.charAt(i++) == '-' ? -1: 1;
        }

        // 
        while (i < str.length() && str.charAt(i) >= '0' && str.charAt(i) <= '9') {
            if (base > Integer.MAX_VALUE / 10 || (base == Integer.MAX_VALUE / 10 && str.charAt(i) - '0' > 7)) {
                return sign == 1 ? Integer.MAX_VALUE : Integer.MIN_VALUE;
            }

            base = base * 10 + (str.charAt(i++) - '0');
        }
        return base * sign;
    }

  func myAtoi(_ s: String) -> Int {
          // 判断有效性
         if s.isEmpty {return 0}
         // 转换值
         var base = 0.0
         // 正负号
         var sign = 1
         // 索引值
         var i = 0
         // 判断空格
         while i < s.count && s[i] == " " {
             i += 1
         }
         // 判断正负号
         if i < s.count && (s[i] == "+" || s[i] == "-")  {
             sign = s[i] == "+" ?  1: -1
             i += 1
         }
         
         while i < s.count && (s[i] >= "0") && (s[i] <= "9") {
             if base > Double(Int32.max) / 10.0 || ((base == Double(Int32.max) / 10.0) && Double(String(s[i]))! > 7.0) {
                 return sign == 1 ? Int.max : Int.min
             }
             base = base * 10.0 + Double(String(s[i]))!
             i += 1
         }
         if sign == -1 {
             base = -1 * base
         }

         return Int(base)
     }
    

      func myAtoi_5(_ str: String) -> Int {
          var index = 0
          for c in str {
              if c == " " {
                  index += 1
              } else {
                  break
              }
          }
          
          var positive = true
          if index < str.count {
              if str[index] == "+" {
                  positive = true
                  index += 1
              } else if str[index] == "-" {
                  positive = false
                  index += 1
              }
          }
          
          var result = 0.0
          while index < str.count {
              if !"0123456789".contains(str[index]) {
                  break
              }
              if result > (Double(Int32.max) - Double(String(str[index]))!) / 10.0 {
                  // 最大的改变就是这里了
                  return positive ? Int(Double(Int32.max)) : Int(Double(Int32.min))
              }
              result *= 10
              result += Double(String(str[index]))!
              index += 1
          }
          
          if !positive {
              result = -result
          }
          
          return Int(result)
      }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值