Giter VIP home page Giter VIP logo

Comments (4)

lifei6671 avatar lifei6671 commented on May 18, 2024

这是我面试的时候学习用的,我给的解法肯定不是最优解法。而且我也不知道你标准的最优解法是啥。。

from interview-go.

TheStarBoys avatar TheStarBoys commented on May 18, 2024

不是我标准的最优解法。。只是说面试考你算法的时候,面试官可能会问你如何优化。。如果说给不出最优解的话,可能分会给你较低一点。

from interview-go.

TheStarBoys avatar TheStarBoys commented on May 18, 2024

比如你在《判断两个给定的字符串排序后是否一致》里面的解法,sl1 := len([]rune(s1)) 这里的转换其实会有点多余,len(s1) 就已经能取到字符串长度了。

再来看算法,表面上是一次迭代,时间复杂度为 O(N),但其实没考虑到 strings.Count 的时间复杂度。要统计一个字符串中的字符出现次数,必定至少会发生完整的一次迭代,所以 Count 操作时间复杂度至少也姑且认为是 O(N)。那总体看来,解法的时间复杂度实际为 O(N^2)。

func isRegroup(s1,s2 string) bool {
	sl1 := len([]rune(s1))
	sl2 := len([]rune(s2))

	if sl1 > 5000 || sl2 > 5000 || sl1 != sl2{
		return false
	}

	for _,v := range s1 {
		if strings.Count(s1,string(v)) != strings.Count(s2,string(v)) {
			return false
		}
	}
	return true
}

那面试官可能会问:“能实现时间复杂度为 O(N) 的算法吗?”

from interview-go.

viper-00 avatar viper-00 commented on May 18, 2024

@TheStarBoys rune 你要考虑字符串出现中文或其他字符的情况。

from interview-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.