zoukankan      html  css  js  c++  java
  • 389. Find the Difference

    Given two strings s and t which consist of only lowercase letters.

    String t is generated by random shuffling string s and then add one more letter at a random position.

    Find the letter that was added in t.

    Example:

    Input:
    s = "abcd"
    t = "abcde"
    
    Output:
    e
    
    Explanation:
    'e' is the letter that was added.
    class Solution {
        public char findTheDifference(String s, String t) {
            List<Character> list = new ArrayList();
            for(char c: t.toCharArray()){
                list.add(c);
            }
            for(char c: s.toCharArray()){
                if(list.indexOf(c) >= 0){
                    list.remove(list.indexOf(c));
                }
            }
            return list.get(0);
        }
    }

    我怎么老把遍历对象搞错卧槽?

    想法是:把t存起来,然后遍历s,发现当前char在t中就remove,最后剩下的就是答案

    class Solution {
        public char findTheDifference(String s, String t) {
            int ss = 0;
            int tt = 0;
            for(char c: s.toCharArray()) ss += (int) c;
            for(char c: t.toCharArray()) tt += (int) c;
            return (char) (tt - ss);
        }
    }

    刚还在那想能不能两个一减,卧槽真能

  • 相关阅读:
    centos 编程环境
    git 安装 使用
    nodejs 笔记
    微信开发
    composer 使用笔记
    一:安装centos 7最小编程环境 xfce桌面
    二: 安装centos服务环境软件mysql httpd php
    我的通用程序规范及说明
    常用js代码集
    三 , lnmp 一键包安装使用
  • 原文地址:https://www.cnblogs.com/wentiliangkaihua/p/12868087.html
Copyright © 2011-2022 走看看