leetcodeBulls and Cows
来源:互联网 发布:windows temp文件夹 编辑:程序博客网 时间:2024/06/07 11:59
//由于本人比较菜,所以先刷easy的===
这道题做出虽然AC了,但是时间很慢呀,后来仔细看了一下,的确可以优化的。我是先处理值和位置都完全一样的,所以遍历了两次,其实可以完全遍历一次,遍历一次需要注意验证,当取了一个值相同位置不相同的时候,那么guess的一个数字占领了secret中一个数字,如果恰好这个secret对应的位置在guess上是值和位置都相同的,那么这里由于前面已经被占领了,所以不行呀,所以每次找到一个值相同,位置不同的时候,需要回头验证一下,这样如果遍历一次的话,时间应该会减少一些吧。
先把今天的代码留下,虽然慢是慢了点,还是AC了,下次再把这道题优化一下。
class Solution { bool visit[1000]={false}; bool AA[1000]={false}; int A=0; int B=0; public: string getHint(string secret, string guess) { string res; int x=secret.size(); int y=guess.size(); if(x == 0 || y == 0) return res; for(int i = 0; i < y;i++) { if (i<=x && secret[i] == guess[i]) { A++; visit[i]=true; AA[i]=true; } } for(int i =0; i < y;i++) { if (AA[i]) continue; int pos = -1; int begin = 0; do { begin = pos; pos=secret.find(guess[i],begin+1); }while(pos!=-1 && visit[pos]); if(pos != -1) { B++; visit[pos]=true; } } char buf1[10]; char buf2[10]; sprintf(buf1,"%d",A); string res1=buf1; sprintf(buf2,"%d",B); string res2=buf2; return res1+"A"+res2+"B"; }};
0 0
- leetcodeBulls and Cows
- 10491 - Cows and Cars
- Cows and Cars
- 10491 - Cows and Cars
- [LeetCode]Bulls and Cows
- [LeetCode] Bulls and Cows
- LeetCode Bulls and Cows
- [leetcode299]Bulls and Cows
- Bulls and Cows
- leetcode | Bulls and Cows
- Bulls and Cows
- LeetCode:Bulls and Cows
- LeetCode -- Bulls and Cows
- leetcode: Bulls and Cows
- [leetcode]Bulls and Cows
- Bulls and Cows
- leetcocd_Bulls and Cows
- Bulls and Cows
- popen函数相关
- Bootstrap table的一些简单使用总结
- bzoj2005 能量采集 数论
- Segue NSTimer 和 Delayed perform
- Mac Eclipse JAVAHL问题
- leetcodeBulls and Cows
- 在ubuntu中安装eclipse for c++
- 你的能力是更适合做微商还是淘宝
- LA 4329 树状数组入门
- 排序算法的种类
- UML之C++类间关系总结
- POJ 3737 UmBasketella (求最大体积圆锥)
- 操作系统:死锁的产生、条件、和解锁
- Spark Working with Key/Value Pairs