1.两数之和:
static void Main(string[] args)
{
// 两数之和
Console.WriteLine("请输入结果数");
int target = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入5个数字 其中5个数字中要有两个数相加等于结果数 按回车输入");
int[] nums = new int[5];
for (int i = 0; i < nums.Length; i++) nums[i] = Convert.ToInt32(Console.ReadLine());
int[] result = DaTa( nums , target);
Console.WriteLine("结果是:");
foreach (int i in result) Console.WriteLine(i+" ");
}
static int[] DaTa(int[] nums,int target)
{
int longin = nums.Length;
int[] indices = new int[2];
for (int i = 0; i < longin -1; i++ )
{
for (int j = i+1; j < longin; j++)
{
if (nums[i] + nums[j] == target )
{
indices[0] = i;
indices[1] = j;
return indices;
}
}
}
return new int[2];//默认值
}
2.回文数:
回文数是指一个数字或字符串,无论从左向右读还是从右向左读,结果是相同的。换句话说,它们是对称的。在数字的情况下,例如121、1221、12321 都是回文数。在字符串的情况下,例如"level"、“radar”、“madam” 都是回文串。如果 x 是一个回文整数,返回 true ;否则,返回 false 。
我们可以通过一些算法来判断一个数字或字符串是否为回文数。对于数字,我们可以将其转换为字符串,然后通过反转字符串来比较是否相同。对于字符串,我们可以比较首尾字符是否相等,并逐步向中间靠拢。
static void Main(string[] args)
{
int hws = 12321;
bool sf =HuiWens( hws );
Console.WriteLine(sf? "是回文数":"不是回文数");
}
static bool HuiWens(int wens)
{
if (wens < 0) return false;
string wensstr = wens.ToString();// 把整数 转换为字符串
int left = 0;
int right = wensstr.Length-1;
while (left < right)
{
if (wensstr[left] != wensstr[right]) return false;
left++;
right--;
}
return true;
}
3.罗马数字转整数:
罗马数字包含以下七种字符: I
, V
, X
, L
,C
,D
和 M
。
字符 数值 I 1 V 5 X 10 L 50 C 100 D 500 M 1000
例如, 罗马数字 2
写做 II
,即为两个并列的 1 。12
写做 XII
,即为 X
+ II
。 27
写做 XXVII
, 即为 XX
+ V
+ II
。
通常情况下,罗马数字中小的数字在大的数字的右边。但也存在特例,例如 4 不写做 IIII
,而是 IV
。数字 1 在数字 5 的左边,所表示的数等于大数 5 减小数 1 得到的数值 4 。同样地,数字 9 表示为 IX
。这个特殊的规则只适用于以下六种情况:
I
可以放在V
(5) 和X
(10) 的左边,来表示 4 和 9。X
可以放在L
(50) 和C
(100) 的左边,来表示 40 和 90。C
可以放在D
(500) 和M
(1000) 的左边,来表示 400 和 900。
给定一个罗马数字,将其转换成整数。
示例 1:
输入: s = "III" 输出: 3
string roman = "III";
int abc = RomanToInt(roman);
Console.WriteLine(abc);
static int RomanToInt(string s)
{
int[] romanValues = new[] { 1, 5, 10, 50, 100, 500, 1000 };
char[] romanChars = new char[] { 'I', 'V', 'X', 'L', 'C', 'D', 'M' };
int result = 0;
for (int i = 0; i <s.Length; i++)
{
int currValue = romanValues[new string(romanChars).IndexOf(s[i])];
if (i + 1 < s.Length && romanValues[new string(romanChars).IndexOf(s[i + 1])] > currValue)
result -= currValue;
else
result += currValue;
}
return result;
}
4.最长公共前缀:
string[] strs = { "flowert", "flow", "flight" };
string longestPrefix = LongestCommonPrefix(strs);
Console.WriteLine(longestPrefix);
static string LongestCommonPrefix(string[] strs)
{
if (strs == null || strs.Length == 0) // 检查数组为空或无元素的情况
{
return "";
}
string prefix = strs[0]; // 将第一个字符串作为初始前
for (int i = 1; i < strs.Length; i++)
{
while (strs[i].IndexOf(prefix) != 0) // 如果当前前缀不是当前字符串的前缀,则缩小前缀长度
{
prefix = prefix.Substring(0, prefix.Length - 1);
if (prefix.Length == 0) // 如果前缀为空,则不存在公共前缀,直接返回空字符串
{
return "";
}
}
}
return prefix;
}
5.有效的括号:
static void Main(string[] args)
{
string str = "()";
bool bllo = Fake(str);
Console.WriteLine(bllo);
}
static bool Fake(string str)
{
Stack<char> stack = new Stack<char>();
foreach (char c in str)
{
if (c == '(' || c =='{' || c == '[')stack.Push(c);
else if (c == ')' || c == '}' || c == ']')
{
if (stack.Count == 0) return false;
// stack.Pop() 表示从栈 stack 中移除栈顶元素,并返回该元素的值。
char top = stack.Pop();
// 在这里,top != '(' 是一个条件判断语句,用于判断栈顶元素 top 是否等于左括号 '('。等于就闭合
if ((c == ')' && top != '(') || (c == '}' && top != '{') || (c == ']' && top != '['))
{
return false;
}
}
}
return stack.Count == 0;
}
6.合并两个有序列表:
public class ListNode {
public int val;
public ListNode next;
public ListNode(int val = 0, ListNode next = null) {
this.val = val;
this.next = next;
}
}
public class Solution {
public ListNode MergeTwoLists(ListNode l1, ListNode l2) {
ListNode dummy = new ListNode(0);
ListNode curr = dummy;
while (l1 != null && l2 != null) {
if (l1.val < l2.val) {
curr.next = l1;
l1 = l1.next;
} else {
curr.next = l2;
l2 = l2.next;
}
curr = curr.next;
}
// 把剩余链表接到合并后的链表尾部
if (l1 != null) {
curr.next = l1;
}
if (l2 != null) {
curr.next = l2;
}
return dummy.next;
}
}
使用示例:
static void Main(string[] args) {
// 创建示例链表
ListNode l1 = new ListNode(1);
l1.next = new ListNode(2);
l1.next.next = new ListNode(4);
ListNode l2 = new ListNode(1);
l2.next = new ListNode(3);
l2.next.next = new ListNode(4);
// 创建解决方案对象
Solution solution = new Solution();
// 合并两个链表
ListNode merged = solution.MergeTwoLists(l1, l2);
// 打印合并后的链表值
while (merged != null) {
Console.WriteLine(merged.val);
merged = merged.next;
}
}
2.数组的写法:
static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
int[] a = { 1, 2, 3 };
int[] b = { 1, 2, 4 };
int[] arrd = MergeArrays(a, b);
foreach (var item in arrd)
{
Console.WriteLine(item);
}
}
static int[] MergeArrays(int[] list1, int[] list2)
{
if (list1 == null) return list1;
if (list2 == null) return list1;
int[] merged = new int[list1.Length + list2.Length];
int i = 0, j = 0, k = 0;
while (i < list1.Length && j< list2.Length)
{
if (list1[i] <= list2[j])
{
merged[k] = list1[i];
i++;
}
else
{
merged[k] = list2[j];
j++;
}
k++;
}
while (i < list1.Length)
{
merged[k] = list1[i];
i++;
k++;
}
while (j < list2.Length)
{
merged[k] = list2[j];
j++;
k++;
}
return merged;
}
7.删除有序数组的重复项:
public class Solution {
public int RemoveDuplicates(int[] nums) {
if (nums.Length == 0) {
return 0;
}
int i = 0;
for (int j = 1; j < nums.Length; j++) {
if (nums[j] != nums[i]) {
i++;
nums[i] = nums[j];
}
}
return i + 1;
}
}