[关闭]
@programeggsoup 2018-07-10T23:58:08.000000Z 字数 4440 阅读 869

LintCode 刷题

java python LintCode 计算机


1. A + B Problem

Write a function that add two numbers A and B.

1.1 Example

Given a=1 and b=2 return 3.

1.2 Challenge

Of course you can just return a + b to get accepted. But Can you challenge not do it like that?(You should not use + or any arithmetic operators.)

1.3 KeyKnowledge

在计算机中,数字是以补码的形式存在的,计算也是用补码来进行计算,计算后的结果也是补码,可以参考图解 Java 位运算

1.4 Java Code

  1. public class Solution {
  2. /**
  3. * @param a: An integer
  4. * @param b: An integer
  5. * @return: The sum of a and b
  6. */
  7. public int aplusb(int a, int b) {
  8. // write your code here
  9. if(b==0){
  10. return a;
  11. }
  12. int xor = a ^ b;
  13. int carry = (a & b) << 1;
  14. return aplusb(xor, carry);
  15. }
  16. }

1.5 Python Code

- Reference: [不用加减乘除做加法.py][2]
- 因为python整形无限大,所以导致不能直接用java魔改,不然会很慢很慢很慢
- Total runtime: 302ms
  1. class Solution:
  2. """
  3. @param a: An integer
  4. @param b: An integer
  5. @return: The sum of a and b
  6. """
  7. def aplusb(self, a, b):
  8. # write your code here
  9. while b != 0:
  10. xor = a ^ b
  11. b = (a & b) << 1
  12. a = xor & 0xFFFFFFFF
  13. return a if a >> 31 == 0 else a - 4294967296

2. Trailing zeros

Write an algorithm which computes the number of trailing zeros in n factorial.

2.1 Example

11! = 39916800, so the out should be 2

2.2 Challenge

O(log N) time

只有2和5碰在一起的时候,才会有0,而5的个数远比0少,只要计算有多少5

25/5 = 5,5/5 = 1;5+1=6

2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25

2.3 JAVA Code

  1. class Solution:
  2. """
  3. @param: n: An integer
  4. @return: An integer, denote the number of trailing zeros in n!
  5. """
  6. def trailingZeros(self, n):
  7. # write your code here, try to do it without arithmetic operators.
  8. if n == 0:
  9. return 0
  10. count = 0
  11. while n > 0:
  12. n /= 5
  13. count += n
  14. return count

2.4 Python Code

  1. public class Solution {
  2. /*
  3. * @param n: An integer
  4. * @return: An integer, denote the number of trailing zeros in n!
  5. */
  6. public long trailingZeros(long n) {
  7. // write your code here, try to do it without arithmetic operators.
  8. if(n == 1) {
  9. return 0;
  10. }
  11. int five = 0; // 2出现的频率永远低于5,所以只要计算5就行了,一个5配一个2,才能有0
  12. for(long i = 5; n / i > 0; i *= 5) {//2,4,5,6,8,10,12,14,15,20
  13. five += n / i;
  14. }
  15. return five;
  16. }
  17. }

6. Merge Two Sorted Arrays

Merge two given sorted integer array A and B into a new sorted integer array.

6.1 Example

A=[1,2,3,4]
B=[2,4,5,6]
return [1,2,2,3,4,4,5,6]

6.2 Challenge

How can you optimize your algorithm if one array is very large and the other is very small?

6.3 JAVA Code

  1. public class Solution {
  2. /**
  3. * @param A: sorted integer array A
  4. * @param B: sorted integer array B
  5. * @return: A new sorted integer array
  6. */
  7. public int[] mergeSortedArray(int[] A, int[] B) {
  8. // write your code here
  9. int[] merge = new int[A.length + B.length];
  10. System.arraycopy(A,0, merge, 0, A.length);
  11. System.arraycopy(B, 0, merge, A.length, B.length);
  12. Arrays.sort(merge);
  13. return merge;
  14. }
  15. }

6.4 Python Code

  1. class Solution:
  2. """
  3. @param A: sorted integer array A
  4. @param B: sorted integer array B
  5. @return: A new sorted integer array
  6. """
  7. def mergeSortedArray(self, A, B):
  8. # write your code here
  9. C = A + B
  10. list.sort(C)
  11. return C

8. Rotate String

Given a string and an offset, rotate string by offset. (rotate from left to right)

8.1 Example

Given "abcdefg".

offset=0 => "abcdefg"
offset=1 => "gabcdef"
offset=2 => "fgabcde"
offset=3 => "efgabcd"

8.2 Challenge

Rotate in-place with O(1) extra memory.

8.3 JAVA Code

  1. public class Solution {
  2. /**
  3. * @param str: An array of char
  4. * @param offset: An integer
  5. * @return: nothing
  6. */
  7. public void rotateString(char[] str, int offset) {
  8. // write your code here
  9. if(offset != 0 && str.length != 0) {
  10. offset %= str.length;
  11. if(offset != 0) {
  12. char[] copy = str.clone();
  13. for (int i = 0; i < offset; i++) {
  14. str[i] = copy[str.length - offset + i];
  15. }
  16. for (int i = offset; i < str.length; i++) {
  17. str[i] = copy[i - offset];
  18. }
  19. }
  20. }
  21. }
  22. }

8.4 Python Code

  1. class Solution:
  2. """
  3. @param str: An array of char
  4. @param offset: An integer
  5. @return: nothing
  6. """
  7. def rotateString(self, str, offset):
  8. # write your code here
  9. if len(str) != 0:
  10. offset %= len(str)
  11. if offset != 0:
  12. a = str[:-offset]
  13. b = str[-offset:]
  14. c = b + a
  15. #str.replace(b+a)
  16. for i, item in enumerate(str):
  17. str[i] = c[i]

37. Reverse 3-digit Integer

Reverse a 3-digit integer.

37.1 Example

Reverse 123 you will get 321.
Reverse 900 you will get 9.

37.2 Java Code

Running time: 757 ms
  1. public class Solution {
  2. /**
  3. * @param number: A 3-digit number.
  4. * @return: Reversed number.
  5. */
  6. public int reverseInteger(int number) {
  7. // write your code here
  8. StringBuilder str = new StringBuilder();
  9. str.append(number);
  10. str = str.reverse();
  11. String result = String.valueOf(str);
  12. if(result.substring(0,1).equals("0")){
  13. result = result.substring(1,result.length());
  14. }
  15. return Integer.parseInt(result);
  16. }
  17. }

37.3 Python Code

Running Time = 252ms
  1. class Solution:
  2. """
  3. @param number: A 3-digit number.
  4. @return: Reversed number.
  5. """
  6. def reverseInteger(self, number):
  7. # write your code here
  8. temp = str(number)
  9. temp = temp[::-1]
  10. if temp[:1] == "0":
  11. temp = temp[2:]
  12. return int(temp)

145. Lowercase to Uppercase

Convert a lowercase character to uppercase.

145.1 Example

a -> A, b -> B ...

145.2 JAVA

Total runtime 3955 ms
  1. public class Solution {
  2. /**
  3. * @param character: a character
  4. * @return: a character
  5. */
  6. public char lowercaseToUppercase(char character) {
  7. // write your code here
  8. return (char)(character - 'a' + 'A');
  9. }
  10. }

145.3 Python

Total runtime 1310 ms
  1. class Solution:
  2. """
  3. @param character: a character
  4. @return: a character
  5. """
  6. def lowercaseToUppercase(self, character):
  7. # write your code here
  8. return character.upper()
添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注