【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
AC代码
C++
1 2 3 4 5 6
| class Solution { public: int sum(int num1, int num2) { return num1 + num2; } };
|
C
1 2 3
| int sum(int num1, int num2){ return num1 + num2; }
|
Python
1 2 3
| class Solution: def sum(self, num1: int, num2: int) -> int: return num1 + num2
|
Python2
1 2 3 4 5 6 7 8
| class Solution(object): def sum(self, num1, num2): """ :type num1: int :type num2: int :rtype: int """ return num1 + num2
|
Java
1 2 3 4 5
| class Solution { public int sum(int num1, int num2) { return num1 + num2; } }
|
C#
1 2 3 4 5
| public class Solution { public int Sum(int num1, int num2) { return num1 + num2; } }
|
Javascript
1 2 3
| var sum = function(num1, num2) { return num1 + num2; };
|
Ruby
1 2 3
| def sum(num1, num2) return num1 + num2 end
|
Swift
1 2 3 4 5
| class Solution { func sum(_ num1: Int, _ num2: Int) -> Int { return num1 + num2 } }
|
Go
1 2 3
| func sum(num1 int, num2 int) int { return num1 + num2; }
|
Scala
1 2 3 4 5
| object Solution { def sum(num1: Int, num2: Int): Int = { num1 + num2 } }
|
Kotlin
1 2 3 4 5
| class Solution { fun sum(num1: Int, num2: Int): Int { return num1 + num2 } }
|
Rust
1 2 3 4 5
| impl Solution { pub fn sum(num1: i32, num2: i32) -> i32 { return num1 + num2; } }
|
PHP
1 2 3 4 5 6 7 8 9 10 11
| class Solution {
function sum($num1, $num2) { return $num1 + $num2; } }
|
TypeScript
1 2 3
| function sum(num1: number, num2: number): number { return num1 + num2 };
|
Racket
1 2 3 4
| (define/contract (sum num1 num2) (-> exact-integer? exact-integer? exact-integer?) (+ num1 num2) )
|
Erlang
1 2 3
| -spec sum(Num1 :: integer(), Num2 :: integer()) -> integer(). sum(Num1, Num2) -> Num1 + Num2.
|
Elixir
1 2 3 4 5 6
| defmodule Solution do @spec sum(num1 :: integer, num2 :: integer) :: integer def sum(num1, num2) do num1 + num2 end end
|
Dart
1 2 3 4 5
| class Solution { int sum(int num1, int num2) { return num1 + num2; } }
|
同步发文于CSDN,原创不易,喜欢了点个赞再走吧原文链接哦~
Tisfy:https://letmefly.blog.csdn.net/article/details/132376238