Files
hello-algo/ja/codes/zig/chapter_searching/two_sum.zig
Yudong Jin 7257641e0e Retranslate the complete Japanese online edition from Chinese (#2008)
* Retranslate the complete Japanese online edition from Chinese

* Fit Japanese homepage labels and localize heading permalinks

* Point Japanese contribution instructions to the Japanese preview

* Replace locale conditionals with multilingual UI tables
2026-10-30 02:12:26 +08:00

59 lines
1.7 KiB
Zig
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// File: two_sum.zig
// Created Time: 2023-01-07
// Author: codingonion (coderonion@gmail.com)
const std = @import("std");
const inc = @import("include");
// 方法 1:総当たり列挙
pub fn twoSumBruteForce(nums: []i32, target: i32) ?[2]i32 {
var size: usize = nums.len;
var i: usize = 0;
// 二重ループのため、時間計算量は O(n^2)
while (i < size - 1) : (i += 1) {
var j = i + 1;
while (j < size) : (j += 1) {
if (nums[i] + nums[j] == target) {
return [_]i32{@intCast(i), @intCast(j)};
}
}
}
return null;
}
// 方法 2:補助ハッシュテーブル
pub fn twoSumHashTable(nums: []i32, target: i32) !?[2]i32 {
var size: usize = nums.len;
// 補助ハッシュテーブルの空間計算量は O(n)
var dic = std.AutoHashMap(i32, i32).init(std.heap.page_allocator);
defer dic.deinit();
var i: usize = 0;
// 一重ループのため、時間計算量は O(n)
while (i < size) : (i += 1) {
if (dic.contains(target - nums[i])) {
return [_]i32{dic.get(target - nums[i]).?, @intCast(i)};
}
try dic.put(nums[i], @intCast(i));
}
return null;
}
pub fn main() !void {
// ======= テストケース =======
var nums = [_]i32{ 2, 7, 11, 15 };
var target: i32 = 9;
// ====== 実行用コード ======
// 方法 1
var res = twoSumBruteForce(&nums, target).?;
std.debug.print("方法 1 res = ", .{});
inc.PrintUtil.printArray(i32, &res);
// 方法 2
res = (try twoSumHashTable(&nums, target)).?;
std.debug.print("\n方法 2 res = ", .{});
inc.PrintUtil.printArray(i32, &res);
_ = try std.io.getStdIn().reader().readByte();
}