LEETCODE面试题01.01
2020-03-06 17:42
771 查看
一,题目:实现一个算法,确定一个字符串 s 的所有字符是否全都不同。
示例 1:
输入: s = “leetcode”
输出: false
示例 2:
输入: s = “abc”
输出: true
二,首先我看到这个题目我想法是:1,首先判断字符串长度是否为零。2,将各字符减去’a’,即可得到对应的数字(0~26),然后去循环遍历整型数组,若有对应的值则+1. 3,循环遍历bucket数组中的元素,如果某值>1,则返回false,否则返回true。
三 代码:
bool isUnique(char* astr) { bool rel = true; //判断是非为零 if(strlen(astr) == 0) { rel =true; } else { int len = strlen(astr); // 建立整形数组 int arr[len]; for(int i=0; i<len,i++) // 字符串转化为 字符数组 { arr[i]= astr[i]-'a'; } int bucket[26] = {0}; for(int j=0; j<len;j++) { bucket[arr[j]]++; } for (int k=0;k<26;k++) //遍历整形数组中各元素是否有大于1的元素 { if(bucket[k] >1) { rel= false; break; } } } return rel ; }
因为刚刚开始所以思路比较简单,易于实现,但是时间复杂度为O(n)。
- 点赞
- 收藏
- 分享
- 文章举报
相关文章推荐
- 【Golang】LeetCode-剑指Offer-面试题61-扑克牌中的顺子【两种解法】
- 【Golang】LeetCode-剑指Offer-面试题32 - II-从上到下打印二叉树 II
- 剑指offer 面试题20:顺时针打印矩阵及其变形(LeetCode54. Spiral Matrix旋转矩阵) 题解
- 剑指offer 面试题39:判断平衡二叉树(LeetCode 110. Balanced Binary Tree) 题解
- 剑指Offer面试题8 & Leetcode153
- leetcode 刷题记录(高频算法面试题汇总)--最小栈
- 算法面试题-leetcode学习之旅(二)
- leetcode微软面试题92Reverse Linked List II
- LeetCode :232. Implement Queue using Stacks(剑指offer面试题)
- 【Golang】LeetCode-剑指Offer-面试题34-二叉树中和为某一值的路径
- 【Golang】LeetCode-剑指Offer-面试题60-n个骰子的点数
- 【Golang】LeetCode-剑指Offer-面试题30-包含min函数的栈
- leetcode--面试题32 - I. 从上到下打印二叉树
- 剑指Offer面试题11 & Leetcode50
- leetcode 刷题记录(高频算法面试题汇总)--两个数组的交集 i & ii
- leetcode及笔试面试题整理序言
- LRU Cache --- LeetCode 面试题
- 子集系列(一) 传统subset 问题,例 [LeetCode] Subset, Subset II, Bloomberg 的一道面试题
- Wildcard Matching (Recursive and Non Recursive method) (Leetcode 网易有道面试题)
- 【Golang】LeetCode-剑指Offer-面试题33-二叉搜索树的后序遍历序列【两种解法】