【LetMeFly】2235.两整数相加:19种语言解法(力扣全解法)
力扣题目链接:https://leetcode.cn/problems/add-two-integers/
给你两个整数 num1
和 num2
,返回这两个整数的和。
示例 1:
输入:num1 = 12, num2 = 5 输出:17 解释:num1 是 12,num2 是 5 ,它们的和是 12 + 5 = 17 ,因此返回 17 。
示例 2:
输入:num1 = -10, num2 = 4 输出:-6 解释:num1 + num2 = -6 ,因此返回 -6 。
提示:
-100 <= num1, num2 <= 100
方法一:返回num1 + num2
- 时间复杂度 O ( 1 ) O(1) O(1)
- 空间复杂度 O ( 1 ) O(1) O(1)
AC代码
C++
class Solution {
public:int sum(int num1, int num2) {return num1 + num2;}
};
C
int sum(int num1, int num2){return num1 + num2;
}
Python
class Solution:def sum(self, num1: int, num2: int) -> int:return num1 + num2
Python2
class Solution(object):def sum(self, num1, num2):""":type num1: int:type num2: int:rtype: int"""return num1 + num2
Java
class Solution {public int sum(int num1, int num2) {return num1 + num2;}
}
C#
public class Solution {public int Sum(int num1, int num2) {return num1 + num2;}
}
Javascript
var sum = function(num1, num2) {return num1 + num2;
};
Ruby
def sum(num1, num2)return num1 + num2
end
Swift
class Solution {func sum(_ num1: Int, _ num2: Int) -> Int {return num1 + num2}
}
Go
func sum(num1 int, num2 int) int {return num1 + num2;
}
Scala
object Solution {def sum(num1: Int, num2: Int): Int = {num1 + num2}
}
Kotlin
class Solution {fun sum(num1: Int, num2: Int): Int {return num1 + num2}
}
Rust
impl Solution {pub fn sum(num1: i32, num2: i32) -> i32 {return num1 + num2;}
}
PHP
class Solution {/*** @param Integer $num1* @param Integer $num2* @return Integer*/function sum($num1, $num2) {return $num1 + $num2;}
}
TypeScript
function sum(num1: number, num2: number): number {return num1 + num2
};
Racket
(define/contract (sum num1 num2)(-> exact-integer? exact-integer? exact-integer?)(+ num1 num2))
Erlang
-spec sum(Num1 :: integer(), Num2 :: integer()) -> integer().
sum(Num1, Num2) ->Num1 + Num2.
Elixir
defmodule Solution do@spec sum(num1 :: integer, num2 :: integer) :: integerdef sum(num1, num2) donum1 + num2end
end
Dart
class Solution {int sum(int num1, int num2) {return num1 + num2;}
}
同步发文于CSDN,原创不易,喜欢了点个赞再走吧原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/132376238