LeetCode(387)First Unique Character in a String

题目

Given a string, find the first non-repeating character in it and return it‘s index. If it doesn‘t exist, return -1.

Examples:

s = "leetcode"
return 0.

s = "loveleetcode",
return 2.

Note: You may assume the string contain only lowercase letters.

分析

求给定字符串中第一个只出现一次的字符。

哈希思想。

代码

/*
387. First Unique Character in a String
*/

#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include <map>

using namespace std;
class Solution {
public:
	/*方法一,借助哈希表*/
	int firstUniqChar1(string s) {
		if (s.empty())
			return -1;

		vector<int> v(256, 0);
		int len = s.length();
		for (int i = 0; i < len; ++i)
			++v[s[i]];

		for (int i = 0; i < len; ++i)
			if (v[s[i]] == 1)
				return i;
		return -1;
	}
	/*方法二:*/
	int firstUniqChar(string s) {
		if (s.empty())
			return -1;
		int len = s.length();
		map<char, int> sm;
		for (int i = 0; i < len; ++i)
		{
			++sm[s[i]];
		}//for

		for (int i = 0; i < len; ++i)
			if (sm[s[i]] == 1)
				return i;
		return -1;
	}

};
时间: 2024-08-06 07:54:42

LeetCode(387)First Unique Character in a String的相关文章

Leetcode(4)寻找两个有序数组的中位数

Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定两个大小为 m 和 n 的有序数组 nums1 和* nums2. 请你找出这两个有序数组的中位数,并且要求算法的时间复杂度为 O(log(m + n)). 你可以假设 nums1* 和 nums2 不会同时为空. 第一种方法:list拼接排列取中位数 执行用时:116 ms : 内存消耗:11.8MB 效果:还行 class Solution(object): def findMedianSortedArrays(self,

Leetcode(5)最长回文子串

Leetcode(4)寻找两个有序数组的中位数 [题目表述]: 给定一个字符串 s,找到 s 中 最长 的回文子串.你可以假设 s 的最大长度为 1000.' 第一种方法:未完成:利用回文子串的特点 一开始我的思路如下:回文子串的特点是首尾字母相同,所以我对每一个字母都找到位于它后面的相同字母,利用切片判断这一段是否为回文子串(str[i:j]==str[i:j][::-1]).时间复杂度很高,主要是因为str.find操作非常耗时. class Solution(object): def lo

Leetcode(1)两数之和

Leetcode(1)两数之和 [题目表述]: 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标.你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个数组中同样的元素. 第一种方法:暴力 执行用时:5352 ms: 内存消耗:12.9MB 效果:非常差 class Solution(object): def twoSum(self, nums, target): """ :type nums:

Leetcode(2)两数相加

Leetcode(2)两数相加 [题目表述]: 给出两个 非空 的链表用来表示两个非负的整数.其中,它们各自的位数是按照 逆序 的方式存储的,并且它们的每个节点只能存储 一位 数字. 如果,我们将这两个数相加起来,则会返回一个新的链表来表示它们的和.您可以假设除了数字 0 之外,这两个数都不会以 0 开头. 第一种方法:大众解法 执行用时:80 ms: 内存消耗:12.2MB # Definition for singly-linked list. # class ListNode(object

Leetcode(3)无重复字符的最长子串

Leetcode(3)无重复字符的最长子串 [题目表述]: 给定一个字符串,请你找出其中不含有重复字符的 最长子串 的长度. 第一种方法:暴力 执行用时:996 ms: 内存消耗:12.9MB 效果:太差 class Solution(object): def lengthOfLongestSubstring(self, s): """ :type s: str :rtype: int """ Maxsize=0 res='' if len(s)

Leetcode(9)回文数

Leetcode(9)回文数 [题目表述]: 判断一个整数是否是回文数.回文数是指正序(从左向右)和倒序(从右向左)读都是一样的整数. 第一次:直接全部转 执行用时:148 ms: 内存消耗:13.4MB 效果:还行 class Solution: def isPalindrome(self, x: int) -> bool: s=str(x) if s==s[::-1]: return True else: return False 第二种方法:反转一半数字 执行用时:156 ms: 内存消耗

Leetcode-387 First Unique Character in a String

#387.   First Unique Character in a String Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You m

LeetCode_387. First Unique Character in a String

387. First Unique Character in a String Easy Given a string, find the first non-repeating character in it and return it's index. If it doesn't exist, return -1. Examples: s = "leetcode" return 0. s = "loveleetcode", return 2. Note: You

LeetCode — (1)

摘要: Nim Game.WordPattern.Move zeros.First Bad version.Ugly Number五个算法的python实现. 一个月多没更新,大概是因为状态一直不太好吧,有几次打开却不知从何写起.总结一下这一个月多:看了几个算法:接触了hadoop虽然还不算会:会用linux:看了HTML,JS:拿了两个省奖,其中一个真是一直的梦想:加入了一个团队也算是离自己的梦想更近一步:去过自己喜欢的地方:吃吃吃玩玩玩:做了好几件自己喜欢的事:帮到了挺多人:此刻却突然纠结于