elgamal算法两种算法
One of the influences I remember in considering software development, was my sister telling me I would be good at it because I’m ‘a really good problem solver’. We enjoyed playing brain teaser and memory related games like ‘Bomb Corp’ by Jackbox Games. Whenever I would impress her during the game with my memory recall or understanding of the tricky rules, she would insist that I could take well to programming. In her experience, software development was ‘all problem solving’. Now that I am a full-stack web developer, and practicing algorithms on leetcode, I can understand and appreciate where she was coming form. You have to engage two fundamental skills in order to solve a coding challenge. The first skill is problem solving, as in, understanding the technical constraints of the problem and how to work towards the solution. The second one is being able to turn that understanding into valid, executable code.
我记得在考虑软件开发方面的影响之一是我姐姐告诉我,我会擅长于此,因为我是“一个非常好的问题解决者”。 我们喜欢玩益智游戏和与记忆有关的游戏,例如Jackbox Games的“ Bomb Corp”。 每当我在游戏中以记忆力的回忆或对棘手规则的理解给她留下深刻印象时,她都会坚持认为我可以很好地进行编程。 根据她的经验,软件开发是“所有问题的解决”。 现在,我是一名全职Web开发人员,并且在leetcode上练习算法,我可以理解和欣赏她的发展方向。 您必须具备两项基本技能才能解决编码难题。 第一个技能是解决问题,例如了解问题的技术约束以及如何努力解决问题。 第二个是能够将这种理解转化为有效的可执行代码。
My first mock technical challenge involved an algorithm that really highlighted the brain-teaser ‘problem solving’ skill of coding. It was as follows:
我的第一个模拟技术挑战涉及一种算法,该算法真正突显了脑筋急转弯的“解决问题”编码技能。 内容如下:
Given any alphabetic string, determine if it is an anagram of a palindrome.
The moment I saw this, I thought it was a fun question. To clarify, an anagram is a string of characters that has been arranged into a new order, without removing or adding any extra characters.
当我看到这一点时,我认为这是一个有趣的问题。 为了说明清楚,字谜是一串以新顺序排列的字符,没有删除或添加任何额外的字符。
An anagram of "cat" is "tac"
A palindrome is a string of characters that has the same order when read forward or backward.
回文是一串向前或向后读取顺序相同的字符。
An example of a palindrome is "racecar". It is spelt the same forwards and backwards.
I had practiced problems asking if two strings are anagrams, where my solution involved making a hash-map and ensuring every character existed in each string the same amount of times. I had also practiced a problem asking if a string is an anagram, and my solution involved using pointers at each end of the string, ensuring as they moved towards the middle, every character matched, and if there was an odd number of characters, the middle could be unique. This problem upped the ante by combining the challenges.
我曾练习过问两个字符串是否为字谜的问题,我的解决方案包括制作一个哈希图,并确保每个字符串中的每个字符存在相同的时间。 我还练习了一个问题,询问一个字符串是否是一个字谜,并且我的解决方案涉及在字符串的每一端使用指针,确保当指针移向中间时,每个字符都匹配,并且如果有奇数个字符,中间可能是唯一的。 这个问题结合了挑战而增加了赌注。
"obb" is an anagram of a palindrome, because "bob" is an anagram of "obb" and "bob" is a palindrome.
Now we are all on the same page. Being able to fluently write code in any language is a useless skill unless you can understand how to solve the problem. So, without thinking about software or code, how do you know if any string of characters could possibly be an anagram of a palindrome? You could rearrange the string of characters into every possible combination and see if they read the same forwards and backwards, but if the string contains more than a few characters, that would take absurdly long. We need to think more practically.
现在我们都在同一页面上。 除非您能理解如何解决问题,否则能够熟练地用任何语言编写代码都是一项无用的技能。 因此,在不考虑软件或代码的情况下,如何知道任何字符串都可能是回文的字谜? 您可以将字符串重新排列为每种可能的组合,并查看它们前后是否读取相同的字符串,但是如果字符串包含多个字符,这将花费很长的时间。 我们需要更实际地思考。
When you look at the following string, how quickly can you tell it is not a palindrome?
当您看下面的字符串时,您多快可以知道它不是回文?
"that"
You can see ‘that’ is not a palindrome fairly instantly, because it doesn’t read the same forward and backward, because there’s only one “a” and one “h”. You can also tell ‘that’ couldn’t be an anagram of a palindrome, because no matter what, in one direction the “a” will always come before the “h”, and in the other direction the “h” will always come before the “a”.
您可以立即看到“那”不是回文,因为它前后读不一样,因为只有一个“ a”和一个“ h”。 您还可以告诉'那'不能是回文的字谜,因为无论如何,在一个方向上,“ a”将始终在“ h”之前,而在另一个方向上,“ h”将始终在在“ a”之前。
If we think critically, we can infer some rules of palindromes. In order for a string to read the same forwards and backwards, every character on one side of the string needs to appear on the other side the same number of times. This means that every character needs to appear an even number of times. There is one exception, if the palindrome has an odd length, then there can be one character that appears an odd number of times, and is located in the middle of the palindrome (like the “e” in “racecar”).
如果我们进行批判性思考,我们可以推断出回文规则。 为了使字符串前后读取相同,字符串一侧的每个字符都需要在另一侧出现相同的次数。 这意味着每个字符需要出现偶数次。 有一个例外,如果回文的长度为奇数,则可能有一个字符出现奇数次,并且位于回文的中间(例如“赛车”中的“ e”)。
With this understanding of the rules in our problem, we can devise a solution. We can count the occurrences of every character in our given string, and if more than one character appears an odd amount of times, we know that it is impossible for the string to be an anagram of a palindrome.
有了对问题规则的理解,我们便可以设计解决方案。 我们可以计算给定字符串中每个字符的出现次数,并且如果有多个字符出现奇数次,我们知道该字符串不可能是回文的字谜。
Now that we have completed the “problem solving” portion of the algorithm, it is time to use our coding skills to translate our solution. The following code is what I submitted as my solution when I was given this prompt.
现在我们已经完成了算法的“问题解决”部分,是时候使用我们的编码技能来翻译我们的解决方案了。 以下代码是在提示我时作为解决方案提交的代码。
function isAnagramOfPalindrome(string){
let numOfChar = {}
for(let char of string){
numOfChar[char] ? numOfChar[char]++ : numOfChar[char] = 1
}
let containsLessThanOneOdd
for(let key in numOfChar){
if(numOfChar[key] % 2 === 1){
if(!containsLessThanOneOdd) containsLessThanOneOdd = true
else return false
}
}
return true
}
In the solution I wrote, I create a hash, I iterate through the string, I set every character in that string as a key in my hash and I set its value to 1. If the character appears again, I increment that value. Once I have completed that task, I initialise a variable. I then iterate through my hash, and assign that variable to “true” the first time I encounter an odd value. If I ever encounter a second odd value, I return false and end the process. Otherwise, I complete my iteration and return true.
在我编写的解决方案中,创建一个哈希,遍历字符串,将字符串中的每个字符设置为哈希中的键,并将其值设置为1。如果再次出现该字符,则将该值递增。 一旦完成该任务,就初始化一个变量。 然后,我遍历哈希,并在第一次遇到奇数值时将该变量分配为“ true”。 如果遇到第二个奇数,则返回false并结束该过程。 否则,我将完成迭代并返回true。
I can’t say that this is the best solution, but it is one that I came up with, and it worked. I was proud of my ability to isolate a condition within the problem, and utilize it to develop a working solution. I can truly understand why my sister was so adamant that critical thinking, memory, and problem solving skills can signal someones ability to take well to programming. You need these skills in order to break problems down to their core and devise optimal solutions. Odds are, if you enjoy brain teasers and puzzles, you’ll probably enjoy solving algorithms once you learn to code. I hope you found this article insightful in how to approach a coding challenge. Thanks for reading!
我不能说这是最好的解决方案,但这是我想出的一个解决方案。 我为能够隔离问题中的状况并利用其开发有效的解决方案而感到自豪。 我能真正理解为什么姐姐如此坚决,以至于批判性思维,记忆力和解决问题的能力可以表明某人擅长编程的能力。 您需要这些技能才能将问题分解为核心并设计最佳解决方案。 奇怪的是,如果您喜欢脑筋急转弯和困惑,一旦学习编码,您可能会喜欢求解算法。 我希望您发现本文对如何应对编码挑战有深刻的见解。 谢谢阅读!
翻译自: https://medium.com/dev-genius/the-two-parts-of-solving-an-algorithm-ef95d3a9b811
elgamal算法两种算法